是否曾經有過這樣的經歷:把一個元素置于另一個元素之上,而希望下面的那個元素成為可點擊的?現在,利用css的pointer-events屬性即可做到。
CSS pointer-events
Pointer-events原本來源于SVG,目前在很多瀏覽器中已經得到體現。不過,要讓任何HTML元素生效還得借助于一點點css。該屬性稱之為pointer-events,基本上可以將它設置為auto,這是正常的行為,而“none”是一個有趣的屬性。
將它應用到一個元素
如果你已經設置一個元素的css屬性為pointer-events: none。它將不會捕獲任何click事件,而是讓事件穿過該元素到達下面的元素,就像這樣:
復制代碼代碼如下:
<style> 
 .overlay { 
 pointer-events: none; 
 } 
</style> 
<div id=”overlay” class=”overlay”></div>
瀏覽器支持
到目前為止,Firefox 3.6+、Safari 4 和Google Chrome支持Pointer-events。我覺得Opera和IE肯定會盡快趕上,我不知道它們的計劃中是否支持它。
小演示
我將Pointer-events行為的演示放在一起,在那里你可以自己測試它。正如你看到的那樣,右邊灰色的盒子阻止單擊下面的鏈接。但是,如果你單擊checkbox對其禁用Pointer-events。下面鏈接的click事件將被觸發。
演示頁完整的代碼如下所示:
復制代碼代碼如下:
<!DOCTYPE html> 
<html lang=”en”> 
<head> 
 <meta charset=”utf-8″> 
 <title>CSS pointer events</title> 
 <style> 
 .container { 
 position: relative; 
 width: 370px; 
 font: 15px Verdana, sans-serif; 
 margin: 10px auto; 
 } 
 .overlay { 
 position: absolute; 
 right: 0px; 
 top: 0; 
 width: 40px; 
 height: 40px; 
 background: rgba(0, 0, 0, 0.5); 
 } 
 .pointer-events-none { 
 pointer-events: none; 
 } 
 </style> 
 <script> 
 window.onload = function () { 
 document.getElementById(“enable-disable-pointer-events”).onclick = function () { 
 document.getElementById(“overlay”).className = “overlay ” + ((this.checked)? “pointer-events-none” : “”); 
 }; 
 }; 
 </script> 
</head> 
<body> 
<div class=”container”> 
 <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>, <a href=”http://twitter.com”>Twitter</a>,             
新聞熱點
疑難解答