简体   繁体   English

jQuery添加/删除类或切换类

[英]jQuery add/remove class or toggle class

Im just curious to know the best practice for either toggling a class or just adding and removing it during a mouseenter/mouseleave state using jquery. 我只是想知道在使用jquery在mouseenter / mouseleave状态下切换类或添加或删除类的最佳实践。 Both seem to work fine im just not to sure which is best to go with. 两者似乎都很好,只是不确定是否最好搭配使用。

Thank you 谢谢

  $('#image1').mouseenter(function() { $('#image1').toggleClass('transform'); $('#image1 .images-color-overlay').toggleClass('transparent'); $('#image1 .images-text').toggleClass('show-images-text'); }); $('#image1').mouseleave(function() { $('#image1').toggleClass('transform show-images-text'); $('#image1 .images-color-overlay').toggleClass('transparent'); $('#image1 .images-text').toggleClass('show-images-text'); }); 
 .images-color-overlay { position: absolute; top: 0; right: 0; bottom: 0; left: 0; background-color: rgba(0, 0, 0, 0.4); -webkit-transition: all 1s ease; transition: all 1s ease; } .images { width: 33.333%; float: left; overflow: hidden; position: relative; } #image1 { background-image: url("http://placehold.it/1000x320"); background-size: cover; background-repeat: no-repeat; background-position: center; width: 100%; height: 100px; -webkit-transition: all 1s ease; transition: all 1s ease; } .images-text { text-align: center; width: 100%; position: absolute; bottom: -20px; color: #fff; font-size: 10px; line-height: normal; -webkit-transition: all 1s; transition: all 1s; } .show-images-text { -webkit-transition: all 1s; transition: all 1s; bottom: 20px; } .transform { -webkit-transform: scale(1.25); transform: scale(1.25); -webkit-transition: all 1s ease; transition: all 1s ease; } .transparent { background-color: rgba(0, 0, 0, 0) !important; -webkit-transition: all 1s ease; transition: all 1s ease; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="images"> <div id="image1"> <div class="images-color-overlay"> <p class="images-text">hidden-text</p> </div> </div> </div> 

Your code is technically fine, however you can shorten it to just use the hover() method, as the function you provide will be called for both mouseenter and mouseleave events. 您的代码在技术上是不错的,但是您可以将其缩短为仅使用hover()方法,因为将为mouseentermouseleave事件调用您提供的函数。

You can also use the this reference in the function to save DOM accesses, and also cache the jQuery object created from $(this) in a variable for re-use. 您还可以在函数中使用this引用保存DOM访问,并将$(this)创建的jQuery对象缓存在变量中以供重复使用。 Try this: 尝试这个:

 $('#image1').hover(function() { var $image = $(this).toggleClass('transform'); $image.find('.images-color-overlay').toggleClass('transparent'); $image.find('.images-text').toggleClass('show-images-text'); }); 
 .images-color-overlay { position: absolute; top: 0; right: 0; bottom: 0; left: 0; background-color: rgba(0, 0, 0, 0.4); -webkit-transition: all 1s ease; transition: all 1s ease; } .images { width: 33.333%; float: left; overflow: hidden; position: relative; } #image1 { background-image: url("http://placehold.it/1000x320"); background-size: cover; background-repeat: no-repeat; background-position: center; width: 100%; height: 100px; -webkit-transition: all 1s ease; transition: all 1s ease; } .images-text { text-align: center; width: 100%; position: absolute; bottom: -20px; color: #fff; font-size: 10px; line-height: normal; -webkit-transition: all 1s; transition: all 1s; } .show-images-text { -webkit-transition: all 1s; transition: all 1s; bottom: 20px; } .transform { -webkit-transform: scale(1.25); transform: scale(1.25); -webkit-transition: all 1s ease; transition: all 1s ease; } .transparent { background-color: rgba(0, 0, 0, 0) !important; -webkit-transition: all 1s ease; transition: all 1s ease; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="images"> <div id="image1"> <div class="images-color-overlay"> <p class="images-text">hidden-text</p> </div> </div> </div> 

Well a lot of this style question get shot down here on SO, because it seems it comes down to preference. 好吧,很多这样的风格问题在SO上都被忽略了,因为它似乎归结为偏好。 But HERE is a way to do it all without javascript, only CSS, which some might consider more efficient. 但是,HERE是一种无需JavaScript即可完成所有工作的方法,而CSS则有些人可能认为效率更高。

 .images-color-overlay { position: absolute; top: 0; right: 0; bottom: 0; left: 0; background-color: rgba(0, 0, 0, 0.4); -webkit-transition: all 1s ease; transition: all 1s ease; } .images { width: 33.333%; float: left; overflow: hidden; position: relative; } #image1 { background-image: url("http://placehold.it/1000x320"); background-size: cover; background-repeat: no-repeat; background-position: center; width: 100%; height: 100px; -webkit-transition: all 1s ease; transition: all 1s ease; } .images-text { text-align: center; width: 100%; position: absolute; bottom: -20px; color: #fff; font-size: 10px; line-height: normal; -webkit-transition: all 1s; transition: all 1s; } #image1:hover { -webkit-transform: scale(1.25); transform: scale(1.25); -webkit-transition: all 1s ease; transition: all 1s ease; } #image1:hover .images-text { -webkit-transition: all 1s; transition: all 1s; bottom: 20px; } .images-color-overlay:hover { background-color: rgba(0, 0, 0, 0) !important; -webkit-transition: all 1s ease; transition: all 1s ease; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="images"> <div id="image1"> <div class="images-color-overlay"> <p class="images-text">hidden-text</p> </div> </div> </div> 

The toggleClass is the bast practice in your case. toggleClass是您的最佳实践。

Internally it's also doing same thing if the class exist then remove it and if not then add it. 在内部,如果该类存在,则将其做同样的事情,然后将其删除,如果不存在,则将其添加。 See it yourself , goto this github link and search for toggleClass . 自己查看,转到此github链接并搜索toggleClass

// Check each className given, space separated list
if (self.hasClass(className)) {
  self.removeClass(className);
} else {
  self.addClass(className);
}

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

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