jquery - jquery animate() 帮助

标签 jquery jquery-animate

当用户打开和关闭图片时,我使用此代码来更改不透明度,不幸的是,当用户单击图像时,不透明度不会停留在 1。有人有答案吗?

$(document).ready(function(){

  $('img#slide').animate({"opacity" : .7})
  $('img#slide').hover(function(){
      $(this).stop().animate({"opacity" : 1})
  }, function(){
      $(this).stop().animate({"opacity" : .7})

  });                


  $('img#slide').click(function(){
    $(this).animate({"opacity" : 1});
  });

});

最佳答案

您需要以某种方式在用户单击时禁用 mouseleave 动画。

常见的方法是添加一个类,并使用 mouseleave 检查该类是否存在。

测试实例: http://jsfiddle.net/KnCmR/

$(document).ready(function () {

    $('img#slide').animate({ "opacity": .7 })

    .hover(function () {
        $(this).stop().animate({ "opacity": 1 })
    }, 
    function () {
        if ( !$(this).hasClass("active") ) {
            $(this).stop().animate({ "opacity": .7 });
        }
    })

    .click(function () {
        $(this).addClass("active");
    });
});

编辑:

如果您希望第二次单击将行为恢复为原始状态,请使用 toggleClass() 而不是 addClass():

        $(this).toggleClass("active");
<小时/>

jQuery 文档:

关于jquery - jquery animate() 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3061694/

相关文章:

javascript - 基于 div id 的多个 jQuery 函数

javascript - PHP 资源上的 Azure 网站 404(未找到)

javascript - 在 JavaScript 中重新启用鼠标滚动

jquery - 无法将数据加载到 mvc 4 中的 jTable 中

jquery - 淡出标签边框

javascript - jQuery:动画宽度/高度,但保持居中

javascript - 使用 jQuery 连续向左滑动动画

javascript - 页面跳起来而不是向上滚动

jquery - 下拉导航的最佳方法

jquery - 切换 div onclick 的不透明度