简体   繁体   English

将鼠标悬停在链接上,图像灰度滤镜

[英]Hover over link, image grayscale filter

I have an image with a link on top of it. 我有一个图片,上面有一个链接。 When i hover over the link, the image must be in grayscale. 当我将鼠标悬停在链接上时,图像必须为灰度。 Here's a screenshot (with the image in color). 这是屏幕截图(彩色图像)。 So when i hover over 'BEKIJK REALISATIES' the image (in the background) must be in grayscale. 因此,当我将鼠标悬停在“ BEKIJK REALISATIES”上时,图像(在背景中)必须为灰度。

I'm working in CMS concrete5, so editing the html is not an option (restrictions). 我正在CMS Concrete5中工作,因此不能编辑html(限制)。

图片

This is the code in javascript (that's not working). 这是javascript中的代码(无效)。

 $(document).ready(function() { $(".link1").mouseenter(function() { $(".image1").css.style.filter = "grayscale(1)"; }); }); 

Edit: here is the html and css 编辑:这是HTML和CSS

 .link1 a{ padding-top: 5px; padding-bottom: 5px; padding-left: 15px; padding-right: 15px; display: inline-block; margin-right: 10px; margin-left: 10px; font-size: 15px !important; background-color: #3c3c3b; color: black !important; } .link1 { margin-top: -120px; position: relative; margin-bottom: 82px; } .image1:hover{ -ms-filter: grayscale(1); -webkit-filter: grayscale(1); -moz-filter: grayscale(1); -o-filter: grayscale(1); filter: grayscale(1); } 
 <div class="col-sm-3"> <div class="image1"> <a href="http://grafomantestsite2.be/index.php/realisaties"> <img src="http://grafomantestsite2.be/application/files/6314/4161/6181/realisatie1.png" alt="realisatie1.png" width="401" height="269" class="ccm-image-block img-responsive bID-83"> </a> </div> <div class="link1"> <p><a href="http://grafomantestsite2.be/index.php/realisaties">BEKIJK REALISATIES</a> </p> </div> </div> 

Try this JSFiddle http://jsfiddle.net/ujw9opob/ 试试这个JSFiddle http://jsfiddle.net/ujw9opob/

$(document).ready(function() {
  $(".link1").mouseenter(function() {
    $("img").addClass("addingGrayScale");
  });
    $(".link1").mouseleave(function() {
    $("img").removeClass("addingGrayScale");
  });
});

CSS CSS

.addingGrayScale {
     -webkit-filter: grayscale(1); /* Webkit */
    filter: gray; /* IE6-9 */
    filter: grayscale(1); /* W3C */
}

Seeing as you can't alter the html, doing it with pure css is trick (since i dont know how link1 is structured with regards to image1). 看到您无法更改html,使用纯CSS做到这一点是技巧(因为我不知道link1关于image1的结构如何)。 That said, this is a working fiddle of what you want. 就是说, 是您想要的东西。

It uses the cross browser way to do this taken from here , which uses the css classes: 它使用跨浏览器的方式来执行此处的操作 ,该方式使用css类:

img.grayscale {
    filter: url("data:image/svg+xml;utf8,&lt;svg xmlns=\'http://www.w3.org/2000/svg\'&gt;&lt;filter id=\'grayscale\'&gt;&lt;feColorMatrix type=\'matrix\' values=\'0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\'/&gt;&lt;/filter&gt;&lt;/svg&gt;#grayscale"); /* Firefox 10+, Firefox on Android */
    filter: gray; /* IE6-9 */
    -webkit-filter: grayscale(100%); /* Chrome 19+, Safari 6+, Safari 6+ iOS */
}

and

img.grayscale.disabled {
    filter: url("data:image/svg+xml;utf8,&lt;svg xmlns=\'http://www.w3.org/2000/svg\'&gt;&lt;filter id=\'grayscale\'&gt;&lt;feColorMatrix type=\'matrix\' values=\'1 0 0 0 0, 0 1 0 0 0, 0 0 1 0 0, 0 0 0 1 0\'/&gt;&lt;/filter&gt;&lt;/svg&gt;#grayscale");
    -webkit-filter: grayscale(0%);
}

and implements it like so: 并像这样实现它:

$(document).ready(function() {
  $(".link1")
  .mouseenter(function() {
    $(".image1").addClass('grayscale').removeClass('disabled');
  })
  .mouseleave(function() {
    $(".image1").addClass('disabled');
  });
});

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

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