码迷,mamicode.com
首页 > 其他好文 > 详细

scroll滚动到一定距离触发事件/返回顶部animate

时间:2017-05-28 12:26:41      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:image   ott   对象   min   otto   lib   margin   pos   oct   

html代码

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{margin: 0; padding: 0;}
        .box{ width: 960px; height: 1800px; background: #F29292; margin: 0 auto; }
        .gotop{ width: 60px; height: 60px; background: red; position: fixed; left: 50%; bottom: 50px; margin-left: 480px; display: none; }
    </style>
    <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(function() {
        //scroll 事件适用于所有可滚动的元素和 window 对象(浏览器窗口)。
        $(window).scroll(function() {
                var scroHei = $(window).scrollTop();//滚动的高度
                if (scroHei > 400) {
                    $(‘.J_goTop‘).fadeIn();
                } else {
                    $(‘.J_goTop‘).fadeOut();
                }
            })
            /*点击返回顶部*/
        $(‘.J_goTop‘).click(function() {
            $(‘body,html‘).animate({
                scrollTop: 0
            }, 600);
        })
    })
    </script>
</head>
 
<body>
<div class="box"></div>
<div class="gotop J_goTop">返回顶部</div>
</body>
 
</html>

  效果图

技术分享

 

scroll滚动到一定距离触发事件/返回顶部animate

标签:image   ott   对象   min   otto   lib   margin   pos   oct   

原文地址:http://www.cnblogs.com/chenmiaosong/p/6915383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!