简体   繁体   English

jQuery禁用css:hover效果

[英]Jquery disables css :hover effect

I have several linked photos that are set to be (opacity: 0.45) except on hover and when clicked (opacity:1). 除了悬停和单击时(不透明度:1),我有几张链接的照片被设置为(不透明度:0.45)。 I also have a JQuery function listening for clicks, which will then change the css of the clicked photo to be (opacity:1) and the rest back to (opacity: 0.45). 我还有一个JQuery函数来监听点击,然后将点击的照片的css更改为(opacity:1),其余更改为(opacity:0.45)。 However once the function runs the hover effect is disabled somehow. 但是,一旦函数运行,悬停效果就会被禁用。 Any ideas? 有任何想法吗?

HTML: HTML:

<a class="img_thumbs"><img src="someimage.jpg"></a>

CSS: CSS:

.img_thumbs:hover {
      opacity:1;
}

.img_thumbs {
      opacity:0.45;
}

JQuery: JQuery的:

$('.img_thumbs').click(function(){
    event.preventDefault();
    $('.img_thumbs').css({'opacity': '0.45'});
    $(this).css({'opacity': '1'});
}):

I have tried: 我努力了:

  • adding the JQuery .hover() effect in and outside of the function 在函数的内部和外部添加JQuery .hover()效果
  • changing the selectors, css, html to a.img_thumbs, individually and together 将选择器,css,html分别和一起更改为a.img_thumbs
  • looking for where the code fails 寻找代码失败的地方

That function also runs an AJAX call and several other listeners, but ive determined they do not have any adverse effects (commented them out), so the problem lies only in the code I've provided. 该函数还运行一个AJAX调用和其他几个侦听器,但是我确定它们没有任何不利影响(将它们注释掉),因此问题仅出在我提供的代码中。

An alternate solution would also be gratefully accepted, Thanks in advance. 非常感谢您接受替代解决方案。

use this: 用这个:

CSS: CSS:

.img_thumbs:hover {
  opacity:1;
}
.active {
      opacity:1 !important;
}
.img_thumbs {
      opacity:0.45;
}

JS: JS:

$('.img_thumbs').click(function(event){
    event.preventDefault();
    $('.img_thumbs').removeClass( "active" )
    $(this).addClass( "active" );
});

The tips is use other active class, and add or remove this class, when click. 提示是使用其他活动类,并在单击时添加或删除该类。

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

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