【问题标题】:JQuery hover function opacity animationjQuery悬停功能不透明动画
【发布时间】:2015-02-26 02:44:45
【问题描述】:

在悬停其他元素时尝试为divs opacity 设置动画。首先我用displaynone/block尝试了它,但它在某处读到它不可能为此进行转换。

这有点复杂,因为这应该适用于具有不同 id 的相同类型的每个元素。 (当图片悬停时,带有标题的图片库将出现在img 元素的底部。)

HTML结构是这样的:

<article id="{PostID}">
    <div class="post-content">
        <a><img></a>
        <div class="post-content--padded">
            <p>Caption of the picture.</p>
        </div>
    </div>
</article>

首先,我将mouseovermouseout 方法添加到post-content div,如下所示:

onmouseover="document.getElementById('{PostID}').getElementsByClassName('post-content--padded')[0].style.opacity='1.0';" onmouseout="document.getElementById('{PostID}').getElementsByClassName('post-content--padded')[0].style.opacity='0.0';"

这行得通,但没有transition。我已经使用转换处理程序设置 CSS 以应用于 post-content--padded 中的所有 css 更改,如下所示:

-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;

这似乎不会影响我所做的mouseovermouseout 不透明度更改,因此我尝试在其中添加.animate(),但没有取得太大成功。好吧,我让post-content 淡入淡出,但不是post-content--padded

不同的方法

这一切都没有那么奏效。所以我尝试使用JQuery 函数hover()。 长话短说,我在有问题的 html 上方添加了这个:

<script type="text/javascript">
$(document).ready(function(){
    $('#{PostID}.post-content').hover(
    function(){ $('#{PostID}.post-content.post-content--padded').stop().animate({'opacity': '1'}, 'slow');},
    function(){ $('#{PostID}.post-content.post-content--padded').stop().animate({'opacity': '0'}, 'slow');}
    );
 });
</script>

这只是不想工作。无休止地浏览stackoverflow 和其他来源似乎对我没有帮助。被困在这个问题上一个多小时,我决定简单地问一下。添加悬停>不透明过渡并不是那么难。

希望我没有说清楚,人们在这里理解我的问题。

【问题讨论】:

  • 悬停时需要显示图片标题
  • 标题在post-content--padded
  • jQuery 方法不起作用,因为 Tumblr 不通过
  • 啊啊啊!那时一定是这样。好吧,我不会替换任何东西..--"

标签: javascript jquery html css tumblr-themes


【解决方案1】:

如果你只需要在悬停时使用 css 就可以做到这一点

.post-content--padded{
    opacity: 0;
    -webkit-transition: all 2s;
    -moz-transition: all 2s;
    transition: all 2s;
}
.post-content:hover .post-content--padded{
    opacity: 1;
    -webkit-transition: all 2s;
    -moz-transition: all 2s;
    transition: all 2s;
}

查看演示HERE

如果你想使用 Jquery

$('.post-content--padded').hide();
$('.post-content').hover(function(){
    $(this).find('.post-content--padded').fadeToggle(2000);
});

见演示HERE

【讨论】:

  • 让我试试看。我尝试使用 css,但它无法按照我想要的方式工作,所以现在尝试这种方法。
  • 那行得通。我仍然想知道为什么 jquery 不起作用,但无论如何这都少了很多麻烦,所以谢谢。
  • @RomanRutkowski 也看看这个jsfiddle.net/b5pyqfjn/1 我在 css 中添加了 max-height,所以也尝试一下 .. 关于 jquery 你应该检查一些事情 .. 1) 将 jquery 包含到你的代码中。 2) 检查您将使用的选择器,因此检查 {PostID} 选择器并用类更改它会更好
  • 好吧,我现在已经在 Tumblr 之外对其进行了测试,它工作正常,所以确实是 Tumblr 搞砸了。
  • 不要使用transition: _all_ 2s。指定应该应用转换的属性,即transition: opacity 2s 代表better performance
【解决方案2】:

我还致力于将悬停与动画结合起来,它的工作原理是这样的:

在 CSS 不透明度中 "a" = 0

在 jQuery 中:

$("#classy").hover(function(){
      $("#classy").animate({
          opacity:"1"
      },200);
  }, function(){
      $("#classy").animate({
          opacity:"0"
      },200);
  });

【讨论】:

    【解决方案3】:

    这是一个有效的 jQuery 方法:

    HTML

    <div id='hover-me'>hover over me</div>
    <div id='change-me'>I change opacity</div>
    

    CSS

    .hide {
        opacity:0;
    }
    

    JS

    $('#hover-me').hover( function() {
        if ($('#change-me').hasClass('hide')) {
            $('#change-me').removeClass('hide', 'slow');
        } else {
            $('#change-me').addClass('hide', 'slow');
        }
    });
    

    jsFiddle Demo

    *这是包含 jQueryUI 的

    【讨论】:

    • 这看起来可行,但是我被告知 Tumblr 不会解析
    猜你喜欢
    • 2011-01-08
    • 2018-01-23
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 2013-04-15
    • 2012-07-27
    • 1970-01-01
    相关资源
    最近更新 更多