简体   繁体   English

滚动到顶部按钮,不淡入/淡出

[英]Scroll to top button not fading in/out

I am trying to create a scroll to top button which fades in when user scrolls down the page. 我正在尝试创建一个滚动到顶部的按钮,当用户向下滚动页面时该按钮会淡入。

I have managed to place the button where I want it as well as making the on click function take me to the top. 我设法将按钮放置在所需的位置,并且使单击功能将我带到顶部。 However the button shows permanently, it does not fade in or out. 但是该按钮永久显示,不会淡入或淡出。

I have following setup: 我有以下设置:

HTML HTML

 <a class="scrollup" href="#">Scroll To Top</a>

this is placed within a paragraph within a div tag. 它被放置在div标签的一个段落中。

Javascript 使用Javascript

<script type="text/javascript" src="../../../../jquery-1.11.0.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery(window).scroll(function(){
    if (jQuery(this).scrollTop() > 100) {
        jQuery('.scrollup').fadeIn();
    } else {
        jQuery('.scrollup').fadeOut();
}
});
// scroll-to-top animate
jQuery('.scrollup').click(function(){
jQuery("html, body").animate({ scrollTop: 0 }, 600);
    return false;
});
});

CSS CSS

.scrollup {
height: 50px;
width: 50px;
opacity:1.0;
padding:10px;
text-align:center;
background: #FFFFFF;
font-weight:bold;
color:#444;
text-decoration:none;
position:fixed;
top:75px;
right:40px;
}

As said above, the button is there permanently and does not fade in after I scroll, nor does it fade out when I click and go to the top of the page. 如上所述,该按钮永久存在,滚动后不会淡入,单击并转到页面顶部时也不会淡出。

Thanks for any input you might have to resolve this. 感谢您的任何输入,您可能需要解决此问题。

You could refactor your code thus: 您可以这样重构代码:

Demo Fiddle 演示小提琴

CSS CSS

.scrollup {
    height: 50px;
    width: 50px;
    opacity:0;
    padding:10px;
    text-align:center;
    background: #FFFFFF;
    font-weight:bold;
    color:#444;
    text-decoration:none;
    position:fixed;
    top:75px;
    right:40px;
    transition:opacity 1s ease-in;
}
.scrollup.visible {
    opacity:1;
}

jQuery jQuery的

jQuery(document).ready(function () {
    jQuery(window).scroll(function () {
        if (jQuery(this).scrollTop() > 100) {
            jQuery('.scrollup').addClass('visible');
        } else {
            jQuery('.scrollup').removeClass('visible');
        }
    });
    // scroll-to-top animate
    jQuery('.scrollup').click(function () {
        jQuery("html, body").animate({
            scrollTop: 0
        }, 600);
        return false;
    });
});

You should set '.scrollup' to display: none;. 您应该将“ .scrollup”设置为显示:none;。
and your css will look like this: 并且您的css将如下所示:

.scrollup {
height: 50px;
width: 50px;
opacity:1.0;
padding:10px;
text-align:center;
background: #FFFFFF;
font-weight:bold;
color:#444;
text-decoration:none;
position:fixed;
top:75px;
right:40px;
}

and make your script like this: 并使脚本如下所示:

jQuery(document).ready(function($){

    jQuery(window).scroll(function() {

    if(jQuery(this).scrollTop() != 0) {
        jQuery(".scrollup").fadeIn("slow");    
    }

    else {
        jQuery(".scrollup").fadeOut("slow");
    }

  });

i guess you have missed some parameters. 我想你错过了一些参数。

var fit = 400;//how slow/fast you want the button to show. fade in time
var fot = 400;//how slow/fast you want the button to hide fade out time

<script type="text/javascript" src="../../../../jquery-1.11.0.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery(window).scroll(function(){
    if (jQuery(this).scrollTop() > 300) {
        jQuery('.scrollup').fadeIn(fit );
    } else {
        jQuery('.scrollup').fadeOut(fot );
}
});
// scroll-to-top animate
jQuery('.scrollup').click(function(){
jQuery("html, body").animate({ scrollTop: 0 }, 600);
    return false;
});
});

Also i have changed if (jQuery(this).scrollTop() > 300 ) { you can change it back to 100 if working fine. 如果(jQuery(this).scrollTop()> 300 ){如果工作正常,您可以将其更改回100。

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

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