简体   繁体   English

在图像上放置CSS透明覆盖层,并在单击图像时使JavaScript函数起作用?

[英]Placing a CSS transparent overlay over image AND making a JavaScript function work when image is clicked?

I'm having trouble with scripting my CSS in such a way that it will let me do the following at the same time : 我在编写CSS脚本时遇到了麻烦,使我可以同时执行以下操作

  • clear the colored overlay (red) when I hover over the 当我将鼠标悬停在
  • make my picture clickable so that it will perform an unrelated JavaScript function 使我的图片可点击,使其执行不相关的JavaScript功能

Currently I can only do only one of these things separately at a time. 目前,我一次只能单独执行其中一项操作。

My first attempt - clearing the colored overlay on hover BUT my JavaScript function will not work: 我的第一次尝试-清除悬停时的彩色叠加层,但我的JavaScript函数将无法正常工作:

 function myFunction() { /..../ } 
 .image { position:relative; width:400px; height:400px; } .image:after { content:'\\A'; position:absolute; width:100%; height:100%; top:0; left:0; background:rgba(256,0,0,0.6); opacity:1; transition: all 0.5s; -webkit-transition: all 0.5s; } .image:hover:after { opacity:0; } .cursor { cursor: pointer} .myImage { background: url("http://i.stack.imgur.com/Sjsbh.jpg") no-repeat 50% 50%; width: 400px; height: 400px; background-size: cover; } 
 <div class="image"> <!--when the "image" class is present, I cannot perform myFunction() by clicking on the picture --> <div class="myImage cursor" onclick="myFunction()"> </div> </div>http://jsfiddle.net/9z26ky19/2429/#run 

My alternate attempt - here my JavaScript function works BUT I no longer have a colored (red) overlay since I got rid of the "image" class: 我的另一种尝试-在这里我的JavaScript函数有效,但是由于我摆脱了“图像”类,因此不再有彩色(红色)覆盖层:

 function myFunction() { /..../ } 
 .image { position:relative; width:400px; height:400px; } .image:after { content:'\\A'; position:absolute; width:100%; height:100%; top:0; left:0; background:rgba(256,0,0,0.6); opacity:1; transition: all 0.5s; -webkit-transition: all 0.5s; } .image:hover:after { opacity:0; } .cursor { cursor: pointer} .myImage { background: url("http://i.stack.imgur.com/Sjsbh.jpg") no-repeat 50% 50%; width: 400px; height: 400px; background-size: cover; } 
 <div> <!--when the "image" class is NOT present, myFunction() JavaScript works BUT I no longer have the colored (red) overlay --> <div class="myImage cursor" onclick="myFunction()"> </div> </div>http://jsfiddle.net/9z26ky19/2429/#run 

How can I script it in a way such that I can have both a colored (red) overlay over my image AND have my JavaScript myFunction work? 如何以一种既可以在图像上覆盖彩色(红色)又可以使JavaScript myFunction起作用的方式编写脚本?

Use pointer-events: none; 使用pointer-events: none; on the :after pseudo element to make it transparent to mouse events: :after伪元素上以使其对鼠标事件透明:

 function myFunction() { console.log('click'); } 
 .image { position: relative; width: 400px; height: 400px; } .image:after { content: '\\A'; position: absolute; width: 100%; height: 100%; top: 0; left: 0; background: rgba(256, 0, 0, 0.6); opacity: 1; transition: all 0.5s; -webkit-transition: all 0.5s; pointer-events: none; } .image:hover:after { opacity: 0; } .cursor { cursor: pointer } .myImage { background: url("http://i.stack.imgur.com/Sjsbh.jpg") no-repeat 50% 50%; width: 400px; height: 400px; background-size: cover; } 
 <div class="image"> <!--when the "image" class is present, I cannot perform myFunction() by clicking on the picture --> <div class="myImage cursor" onclick="myFunction()"> </div> </div>http://jsfiddle.net/9z26ky19/2429/#run 

How about simply moving the click handler to the parent .image <div> ? 简单地将点击处理程序移动到父.image <div>怎么样?

 function myFunction() { console.log('clicked'); } 
 .image { position:relative; width:400px; height:400px; } .image:after { content:'\\A'; position:absolute; width:100%; height:100%; top:0; left:0; background:rgba(256,0,0,0.6); opacity:1; transition: all 0.5s; -webkit-transition: all 0.5s; } .image:hover:after { opacity:0; } .cursor { cursor: pointer} .myImage { background: url("http://i.stack.imgur.com/Sjsbh.jpg") no-repeat 50% 50%; width: 400px; height: 400px; background-size: cover; } 
 <div class="image" onclick="myFunction()"> <div class="myImage cursor"> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM