简体   繁体   English

悬停和单击时切换类

[英]toggle class on hover and on click

here is my fiddle 这是我的小提琴

I have a click function (item) that shows 'item-overlay' but i'd like to also add a hover to preview the div 'item-overlay' by 100px I added a mouseenter and mouseleave with height to my current code but this then works on click ?! 我有一个显示“ item-overlay”的点击函数(item),但我也想添加一个悬停以将div“ item-overlay”预览100px,我在当前代码中添加了mouseenter和mouseleave高度然后可以点击?! this is my click function - 这是我的点击功能-

     }).on('click', '.item', function (e) {
    (".item click");
    if ($(this).closest('.timelineTile').hasClass("clicked")) {
        e.stopPropagation();
        $(this).siblings('.item-overlay').slideToggle('fast');


    }

and an example of what i tried to do - 以及我尝试做的事的一个例子-

    }).on("mouseenter", ".item", function(e) {
    if ($(this).closest('.timelineTile').hasClass("clicked")) {
    e.stopPropagation();
     $(this).siblings('.item-overlay').height(100); }

    }).on("mouseleave", ".item", function(e) {
     if ($(this).closest('.timelineTile').hasClass("clicked")) {
    e.stopPropagation();
    $(this).siblings('.item-overlay').height(0); }

The problem is that your other open/close animations use hide and show , which also alter the display style. 问题在于您的其他打开/关闭动画使用了hideshow ,这也会改变display样式。

Try this JSFiddle: http://jsfiddle.net/TrueBlueAussie/w4v01t46/3/ 试试这个JSFiddle: http : //jsfiddle.net/TrueBlueAussie/w4v01t46/3/

}).on("mouseenter", ".item", function (e) {
    if ($(this).closest('.timelineTile').hasClass("clicked")) {
        e.stopPropagation();
        $(this).siblings('.item-overlay').css({display: "block"}).height(100);
    }
}).on("mouseleave", ".item", function (e) {
    if ($(this).closest('.timelineTile').hasClass("clicked")) {
        e.stopPropagation();
        $(this).siblings('.item-overlay').hide().height('');
    }
});

notes: 笔记:

  • height('') will reset the height css to inherit (ie no value). height('')将重置高度css以继承(即没有值)。

If you want the height reset when you click again set it with the slideToggle call: 如果要在再次单击时重置高度,请通过slideToggle调用进行设置:

}).on('click', '.item', function (e) {
    (".item click");
    if ($(this).closest('.timelineTile').hasClass("clicked")) {
        e.stopPropagation();
        $(this).siblings('.item-overlay').height('').slideToggle('fast');
    }

JSFiddle: http://jsfiddle.net/TrueBlueAussie/w4v01t46/5/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/w4v01t46/5/

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

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