Add Class on ready, remove class on hover

大城市里の小女人 提交于 2020-01-17 05:46:13

问题


I'm creating my portfolio website. I'm looking to add class on document ready, and remove/change that class to a different class on hover. I'm using lightgallery & CSS gram filters to my images on load and hover.

$(document).ready(function() {
$("#gallery li a").load(function(){
    $($(this).find("img")[0]).addClass("inkwell");
});
$("#gallery li a").hover(function(){
    $($(this).find("img")[0]).removeClass("inkwell").addClass("mayfair");
}); });

the jQuery code above didn't seem to work well.

Please help, Thanks.


回答1:


An anchor doesn't load, it's just there from the start and has no external resource to load, so there's no onload handler

$(document).ready(function() {
    $("#gallery li a img").addClass("inkwell");

    $("#gallery li a").on({
      mouseenter : function() {
        $(this).find("img").removeClass("inkwell").addClass("mayfair");
      },
      mouseleave : function() {
        $(this).find("img").removeClass("mayfair").addClass("inkwell");
      }
    }); 
});

CodePen



来源:https://stackoverflow.com/questions/39943175/add-class-on-ready-remove-class-on-hover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!