简体   繁体   English

jQuery fadeOut在Tumblr上不会消失-它只是消失了

[英]jQuery fadeOut won't fade on Tumblr - it simply disappears

I have installed an infinite scroll script to my Tumblr blog, and am now in the process of adding a scroll-to-top button. 我已经在Tumblr博客中安装了无限滚动脚本,现在正在添加滚动至顶部的按钮。 I want this button to fade in once the user scrolls down past a certain point, and fade out when they scroll back up. 我希望此按钮在用户向下滚动超过某个点时淡入,并在用户向上滚动时淡出。 I also want it to provide a smooth scroll, not just a jump to the top. 我还希望它提供平滑的滚动,而不仅仅是跳转到顶部。

I am fluent with HTML and CSS, though I unfortunately know basically nothing about JavaScript and jQuery. 我很精通HTML和CSS,但不幸的是我基本上对JavaScript和jQuery一无所知。 I found this tutorial for the JS side of things which taught me how to get the desired scroll button. 我在JS方面找到了本教程 ,它教会了我如何获得所需的滚动按钮。 Everything worked great, but the only problem is that the fadeOut doesn't work - the element simply disappears. 一切正常,但唯一的问题是fadeOut不起作用-元素只是消失了。 Sometimes, if I am lucky, it will start fading out a little for a microsecond or so, but then disappear. 有时候,如果幸运的话,它会开始褪色约一微秒,然后消失。

Here is the JavaScript I'm using: 这是我正在使用的JavaScript:

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>

<script>
$(function () {
    $("#gotop")
        .hide();
    $(window).scroll(function () {
        if ($(this).scrollTop() > 400) {
            $('#gotop').fadeIn(500);
        } else {
            $('#gotop').fadeOut(500);
        }
    });
    $('#gotop').click(function () {
    $('html,body').animate({
        scrollTop: 0
    }, 400);
    return false;
  });
});
</script>

I have a simple anchor element in my HTML, styled in CSS using the id #gotop: 我的HTML中有一个简单的anchor元素,使用id #gotop在CSS中设置了样式:

<a href="#top" id="gotop">Top</a>

CSS: CSS:

#gotop {position: fixed;
    right: 2em; bottom: 2em;}

As it is, everything works fine, except for the fade out. 照原样,除了淡入淡出,其他所有东西都可以正常工作。

I've browsed the internet for similar such issues. 我已经在互联网上浏览了类似的问题。 I tried various things which I came across, though most of it was greek to me unfortunately. 我尝试了各种各样的事情,尽管不幸的是,大多数事情对我来说都很希腊。

Edit: I just had a thought. 编辑:我只是有一个想法。 Is it possible that the fade out doesn't occur, because before it has time to fade out, the page has already scrolled back above the "hidden" zone and the element is immediately set to be hidden? 是否有可能不会发生淡出,因为在有时间淡出之前,页面已经向后滚动到“隐藏”区域上方,并且该元素立即设置为隐藏?

If anybody knows anything, it'd be much appreciated - thanks for your time! 如果有人知道什么,将不胜感激-感谢您的宝贵时间!

Hope this helps 希望这可以帮助

Js Fiddle Demo Js小提琴演示

$(function () {
 $('#gotop').hide();
$(window).scroll(function () {

        if ($(this).scrollTop() > 400) {
            $('#gotop').fadeIn(500);
        } else {
            $('#gotop').fadeOut(500);
        }
    });
    $('#gotop').click(function () {
    $('html,body').animate({
        scrollTop: 0
    }, 400);
    return false;
  });
     });

Okay, after some mucking around I managed to figure out the problem. 好吧,经过一番摸索之后,我设法找出了问题所在。

It turns out that I had set all elements on my page to have a CSS3 transition assigned to them, through use of the * selector: 事实证明,通过使用*选择器,我已经将页面上的所有元素都设置为具有CSS3过渡分配给它们:

* {margin: 0; padding: 0;
    transition: all 0.5s;
    -webkit-transition: all 0.5s;
    -moz-transition: all 0.5s;
    -o-transition: all 0.5s;}

I did this for convenience so that any hover I had would have a nice transition. 我这样做是为了方便,这样我进行的任何悬停都会有一个很好的过渡。 However, it seems that this was what was causing my scroll-to-top button to misfunction! 但是,这似乎是导致我的滚动至顶部按钮发生故障的原因!

I would suggest that anybody who is having the same problem as my checks any transitions they've used, and ensure that they're not affecting the to-top button. 我建议与我有相同问题的任何人都要检查他们使用的所有转换,并确保它们不会影响“顶部”按钮。 If there are any doubts, try removing them temporarily just to check. 如果有任何疑问,请尝试暂时将其删除以进行检查。

Hope this helps. 希望这可以帮助。

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

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