js 滚到页面顶部

一、滚到顶部,且滚动中,用户滚动鼠标无效

    <style>
        .div1, .div2, .div3, .div4 {
            height: 400px;
            width: 400px;
        }

        .div1 {
            background: #eea7cf;
        }

        .div2 {
            background: #a95ee1;
        }

        .div3 {
            background: #c57cad;
        }

        .div4 {
            background: #790d86;
        }

        .fixed-tool {            position: fixed;
            top: 50px;
            right: 0;
            display: none;
            /*border: 1px solid black;*/
        }

        .fixed-tool > a {
            display: block;
        }

        .go-top {
            background: #bb9cff;
            padding: 10px 1px;
        }

        .go-top-with-img {
            padding: 0;
            width: 40px;
            height: 40px;
            background: url(top_bg.png) no-repeat;
        }

        .go-top-with-img:hover {
            background-position: left -40px;
        }
    </style>
</head>
<body>
<h1 id="title1">标题1 </h1>

<div class="div1"></div>
<h1>标题2</h1>

<div class="div2"></div>
<h1>标题3</h1>

<div class="div3"></div>
<h1>标题4 </h1>

<div class="div4"></div>
<h1>标题5 </h1>

<div class="div1"></div>
<h1>标题6 </h1>

<div class="div3"></div>
<h1>标题7</h1>

<div class="div2"></div>
<h1>标题8 </h1>
<br/>

<div class="fixed-tool" id="fixedTool">
    <a href="#title1">标题1</a>
    <a href="javascript:;" class="go-top">顶部</a>
    <a href="javascript:;" class="go-top-with-img" id="goTop"></a>
</div>
<script>
    //"use strict";
    window.onload = function () {
        var oGoTopLink = document.getElementById("goTop");
        var iClientHeight = document.documentElement.clientHeight;
        var fixedTool = document.getElementById("fixedTool");
        var timer = null;

        window.onscroll = function () {
            //判断是否滚到了第二屏,是则显示,否则隐藏
            var sScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
            if (sScrollTop >= iClientHeight) {
                fixedTool.style.display = "block";
            } else {
                fixedTool.style.display = "none";
            }
        };

        oGoTopLink.onclick = function () {
            timer = setInterval(function () {
                var sScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
                var iSpeed = Math.floor(-sScrollTop / 12);
                document.body.scrollTop = document.documentElement.scrollTop = sScrollTop + iSpeed;
                document.body.onmousewheel = function(){
                    return false;
                };
                if (sScrollTop <= 0) {
                    clearInterval(timer);
                    document.body.onmousewheel = function(){
                        return true;
                    };
                }
            }, 30);

        }
    };
</script>
</body>
</html>

封装此方法:

//封装以上方法:
    /**
     * @param {String} sWrapId :the element which wraped the go-to-top link
     * @param {String} sEleId :the go-to-top element
     * @param {Number} iSpeed : speed of scrolling ,smaller faster
     * @returns {undefined}
     * usage: goTop("fixedTool", "goTop", 12); 关于样式:可以自己写,如果想用默认样式,就用我上述例子的那些class name,
     */
    function goTop(sWrapId, sEleId, iSpeed){
        var oGoTopLink = document.getElementById(sEleId);
        var iClientHeight = document.documentElement.clientHeight;
        var wrapBox = document.getElementById(sWrapId);
        var timer = null;

        window.onscroll = function () {
            //判断是否滚到了第二屏,是则显示,否则隐藏
            var sScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
            if (sScrollTop >= iClientHeight) {
                wrapBox.style.display = "block";
            } else {
                wrapBox.style.display = "none";
            }
        };

        oGoTopLink.onclick = function () {
            timer = setInterval(function () {
                var sScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
                var iScrollSpeed = Math.floor(-sScrollTop / iSpeed);
                document.body.scrollTop = document.documentElement.scrollTop = sScrollTop + iScrollSpeed;
                document.body.onmousewheel = function(){
                    return false;
                };
                if (sScrollTop <= 0) {
                    clearInterval(timer);
                    document.body.onmousewheel = function(){
                        return true;
                    };
                }
            }, 30);
        };
        return undefined;
    }

二,滚到顶部,且滚动中,若用户滚动鼠标,则停止滚到顶部动作

<script>
    //"use strict";
    window.onload = function () {
        var oGoTopLink = document.getElementById("goTop");
        var iClientHeight = document.documentElement.clientHeight;
        var fixedTool = document.getElementById("fixedTool");
        var bIsTop = true;
        var timer = null;

        //当正在滚回顶部的动作中,用户滚动滚轮的话,停止滚回顶部的动作
        window.onscroll = function () {
            //判断是否滚到了第二屏,是则显示,否则隐藏
            var sScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
            if (sScrollTop >= iClientHeight) {
                fixedTool.style.display = "block";
            } else {
                fixedTool.style.display = "none";
            }
            if (!bIsTop) {
                clearInterval(timer);
            }
            bIsTop = false;
        };

        oGoTopLink.onclick = function () {
            timer = setInterval(function () {
                var sScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
                var iSpeed = Math.floor(-sScrollTop / 12);
                document.body.scrollTop = document.documentElement.scrollTop = sScrollTop + iSpeed;
                bIsTop = true;
                if (sScrollTop <= 0) {
                    clearInterval(timer);
            }, 30);
        }
    };
</script>
时间: 2024-08-07 00:18:54

js 滚到页面顶部的相关文章

js网页返回页面顶部的小方法

咳咳,在网页出现滚动条的时候,许多网站会在右下角出现一个图标,点击可以回到页面顶部 本文就记录下js实现代码: 1.在html页面body添加dom元素 <img src="toTop.png" class="gotop" > <div class="gotopdiv" style="display: none;"><span>返回顶部</span></div> 2.

常用小Demo:用js/jQuery实现回到页面顶部功能

HTML代码: <div class="main">     Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ab amet debitis, dolore eveniet illo maiores nam neque nesciunt perspiciatis praesentium quaerat qui, quidem rem sequi similique sunt ullam voluptate 

js实现简单的匀速回到页面顶部。

<!DOCTYPE html><html> <head> <meta charset="utf-8"/> <title>回到顶部</title> <style> body{ height:1600px; } .top{ width:100%; height: 30px; background:red; } #box{ width:100px; height:30px; background: pink;

网站点击导航栏标题,对应的元素平滑上移以及点击图片或按钮回到页面顶部的特效

之前看w3cshool上面,看到了a,name配合可以设置锚点,很多看书的网站上都用到,可以根据点击对应的目录章节,自动跳到具体的文字内容. 不过用锚点做的特效感觉很粗糙,不够平滑,总是一下子就跳到下面去了,没有给浏览者一种平滑的感觉.所以自己就计划着能不能做一种类似的比较平滑的效果呢? 当然是可以的.这篇博客中的特效要用到如下的函数和属性,大家没见过或者见过用过已经忘了的,可以到w3cshool上恶补下: eq(index):相当于数组吧,可以根据index索引到具体的对象 scrollTo(

用jquery实现小火箭到页面顶部的效果

恩,不知道之前在哪看过一个页面效果就是如果页面被滑动了就出现一个小火箭,点击这个小火箭就可以慢慢回到页面顶部,闲的没事,自己搞了一下 代码和布局都很简单 <!DOCTYPE HTML> <html> <head> <style> #content{ height: 2000px ; width: 960px ; margin: 0 auto ; background-color: blue ; } #rocket{ width: 50px ; height:

selenium + python 怎样才能滚到页面的底部?

可以用 execute_script方法来处理这个. 调用原生javascript的API,这样你想滚到哪里就能滚到哪里. 下面的代码演示了如何滚到页面的最下面: driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") window : JS window 对象 window的scrollTo 方法: 可以滚到页面上的任何位置. scrollHeight:是dom元素的通用属性.document.

vue做的项目每次打开新页面不会显示页面顶部的解决办法

在main.js 中添加代码: 1 router.afterEach((to,from, next) => { 2 3 window.scrollTo(0,0) 4 5 }) 然后就会发现每次打开页面都是显示的是页面顶部内容,再也不会显示页面底部或者其他位置影响用户体验. 原文地址:https://www.cnblogs.com/zr123/p/11929133.html

Node.js静态页面展示例子2

例程下载:https://files.cnblogs.com/files/xiandedanteng/nodejsStaticHtmlSample.rar 页面效果: Html页面代码(注意用文本编辑器如Editplus3保存文件时要指定编码为UTF-8,否则容易出现乱码): <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="

JS判断页面是否出现滚动条

今天无聊,帮一个网友解决一个很无聊的问题,用JS判断页面是否出现滚动条,在网上看了一些代码,经过验证并不起作用,下面是在网上搜索到的代码: 当可视区域小于页面的实际高度时,判定为出现滚动条,即: if (document.documentElement.clientHeight < document.documentElement.offsetHeight) scroll = true; 要使用 document.documentElement ,必须在页面头部加入声明: <!DOCTYPE