jquery - 悬停时保持 div 旋转

标签 jquery html css

我有这个 html 代码:

<div id="To-Top"> <img src="/to-top.png" alt="Volver Arriba" /></div>

这是CSS:

#To-Top { width: 30px; height: 28px;
-webkit-transition-duration: 0.8s;
-moz-transition-duration: 0.8s;
-o-transition-duration: 0.8s;
transition-duration: 0.8s;
-webkit-transition-property: -webkit-transform;
-moz-transition-property: -moz-transform;
-o-transition-property: -o-transform;
transition-property: transform;
overflow:hidden;}
.rotado{
    -webkit-transform:rotate(180deg);
    -moz-transform:rotate(180deg);
    -ms-transform:rotate(180deg);
    transform:rotate(180deg);
    -o-transform:rotate(180deg);
}

还有 jQuery:

jQuery(document).ready(function(){

// hide #back-top first
jQuery("#To-Top").hide();

// fade in #back-top
jQuery(function () {
    jQuery(window).scroll(function () {
        if (jQuery(this).scrollTop() > 400) {
            jQuery('#To-Top').fadeIn();
        } else {
            jQuery('#To-Top').fadeOut();
        }
    });

    // scroll body to 0px on click
    jQuery('#To-Top img').click(function () {
        jQuery('body,html').animate({
            scrollTop: 0
        }, 800);
        return false;
    });
});

});

    jQuery(document).ready(function(){
        jQuery("#To-Top").hover(function () {
            jQuery(this).addClass("rotado");
        }, function () {
            jQuery(this).removeClass("rotado");
        });
    });

出于某种原因,在 Firefox 和 IE 中都可以正常工作,但在 Chrome 中执行动画后,即使悬停被保持,元素也会返回到初始位置。

感谢所有的帮助。

最佳答案

toggleClass can have two parameters ,一个用于悬停,另一个用于悬停:

jQuery(document).ready(function(){
    jQuery("#To-Top").hover(function () {
        jQuery(this).addClass("rotado");
    }, function () {
        jQuery(this).removeClass("rotado");
    });
});

JsFiddle

关于jquery - 悬停时保持 div 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22360670/

相关文章:

javascript - Bootstrap 3 Typeahead 不出现

jquery - eventDrop 在 fullCalendar 中

css - 链接主题的 jqgrid customformatter 不可见

c# - 我可以使用此代码通过对话框登录吗?

javascript - 复选框不会在视觉上取消选中

javascript - Show hide div with addClass removeClass 工作,但只是一秒钟

javascript - 使用插件将所有背景图像的不透明度设置为 0

javascript - 选择菜单时修复背景图像

html - 如何像表格一样设置我的无序列表的样式?

html - CSS IE8 和 H2 的隐形顶部