2014-01-11 161 views

回答

0

有三種可能性:

  1. 您需要將兩個元素都組合到一個公共的<g>元素中。懸停在元素上只能影響元素本身和子元素。
  2. 您可以使用JavaScript
  3. 你可以使用SMIL

CSS

<style type="text/css"> 
    #group:hover #to_be_colored_rect { 
    fill:red 
    } 
    /* If you don't want that the color changes when 
    hovering over to_be_colored_rect, then use this: */ 
    #to_be_colored_rect { 
    pointer-events:none; 
    } 
</style> 
<g id="group"> 
    <rect width="20" height="20"/> 
    <rect id="to_be_colored_rect" width="20" height="20" y="20"/> 
</g> 

的JavaScript

快速和骯髒的:

<rect width="20" height="20" 
    onmousemove="document.getElementById('to_be_colored_rect').setAttribute('fill','red')" 
    onmouseout="document.getElementById('to_be_colored_rect').removeAttribute('fill')"/> 
<rect id="to_be_colored_rect" width="20" height="20" y="20"/> 

(請將mousemove/mouseout事件處理爲生產單獨的腳本,這恰恰說明了基本原理。)

SMIL

<rect width="20" height="20" id="sensitive_rect"/> 
<rect width="20" height="20" y="20"> 
    <set attributeName="fill" attributeType="CSS" to="red" 
    begin="sensitive_rect.mousemove" end="sensitive_rect.mouseout"/> 
</rect> 

這是國際海事組織一個非常優雅的解決方案,但不幸的是,SMIL是不是很一貫支持。

+0

謝謝大家!我明白了......你們都很有幫助!感謝:D – ChrisG

0

我想你可以簡單地改變第一個矩形的填充屬性,當你把鼠標懸停在第二個矩形上。

HTML

<rect onmouseover="turnred()" onmouseout="turnwhite()" id="margin_left" width="40" height="420"/> // Replace line 7 with this line 

的Javascript

function turnred() { 
    document.getElementById("margin_bottom").style.fill="red"; 
} 

function turnwhite() { 
    document.getElementById("margin_bottom").style.fill="white"; 
} 
0

這裏有一個工作頁面:http://cssdeck.com/labs/4r8hitea 基本上是相同的代碼作爲世界光明的使用jQuery

+0

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – honk