js教程

jquery返回网站顶部方法大全

我的站长站 2022-11-16 人阅读

1、基础版,只带返回顶部功能

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>返回顶部</title>
    <script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
    <style type="text/css">
       .container { width:980px; margin:0 auto; height:auto; min-height:100%; position:relative; }
       .content { height: 2000px; border: 1px solid red; }
       #goToTop { position: fixed; bottom: 20px; right: 10%; }
      #goToTop a { background: none repeat scroll 0 0 #336699; border: 1px solid #CCCCCC; border-radius: 3px; -webkit-border-radius: 3px; color: #FF9966; font-size: 14px; padding: 5px; text-decoration: none; text-shadow: 0 1px 0 #999; -webkit-text-shadow: 0 1px 0 #999; }
     </style>
</head>
<body>
    <div>
        <div> 我是头部</div>
        <div>我是主内容,高度是2000px</div>
        <div>我是在最底部</div>
        <div id="goToTop"><a href="javascript:;">点我回到页面顶部</a></div>
    </div>
    <script>
    // 原始版
    $(function(){
        $('#goToTop a').click(function(){
            $('html , body').animate({scrollTop: 0},'slow');
        });
    });
    </script>
</body>
</html>

2、基础升级版,默认不显示,滚动到一定距离显示

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>返回顶部</title>
    <script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
    <style type="text/css">
       .container { width:980px; margin:0 auto; height:auto; min-height:100%; position:relative; }
       .content { height: 2000px; border: 1px solid red; }
       #goToTop { position: fixed; bottom: 20px; right: 10%; }
      #goToTop a { background: none repeat scroll 0 0 #336699; border: 1px solid #CCCCCC; border-radius: 3px; -webkit-border-radius: 3px; color: #ff0; font-size: 14px; padding: 5px; text-decoration: none; text-shadow: 0 1px 0 #999; -webkit-text-shadow: 0 1px 0 #999; }
     </style>
</head>
<body>
    <div>
        <div> 我是头部</div>
        <div>我是主内容,高度是2000px</div>
        <div>我是在最底部</div>
        <div id="goToTop"><a href="javascript:;">点我返回顶部</a></div>
    </div>
    <script>
    // 改进版
    $(function(){
        $('#goToTop').hide();        //隐藏go to top按钮
        $(window).scroll(function(){
            // console.log($(this).scrollTop());
            //当window的scrolltop距离大于1时,go to
            if($(this).scrollTop() > 100){
                $('#goToTop').fadeIn();
            }else{
                $('#goToTop').fadeOut();
            }
        });
        $('#goToTop a').click(function(){
            $('html ,body').animate({scrollTop: 0}, 300);
            return false;
        });
    });
    </script>
</body>
</html>

3、兼容性版,加强浏览器兼容性

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>返回顶部</title>
    <script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
    <style type="text/css">
       .container { width:980px; margin:0 auto; height:auto; min-height:100%; position:relative; }
       .content { height: 2000px; border: 1px solid red; }
       #goToTop {position: absolute; right: -130px; z-index: 9000; }
      #goToTop a { background: none repeat scroll 0 0 #336699; border: 1px solid #CCCCCC; border-radius: 3px; -webkit-border-radius: 3px; color: #ff0; font-size: 14px; padding: 5px; text-decoration: none; text-shadow: 0 1px 0 #999; -webkit-text-shadow: 0 1px 0 #999; }
     </style>
</head>
<body>
    <div>
        <div> 我是头部</div>
        <div>我是主内容,高度是2000px</div>
        <div>我是在最底部</div>
    </div>
    <script>
    // 加强版(兼容性基本完好)
    $(function(){
        //goToTop距浏览器顶端的距离,这个距离可以根据你的需求修改
        var topDistance = 500;
        //距离浏览器顶端多少距离开始显示goToTop按钮,这个距离也可以修改,但不能超过浏览器默认高度,为了兼容不同分辨率的浏览器,我建议在这里设置值为1;
        var showDistance = 1;
        //定义一个变量,方便后面加入在html元素标签中插入这个goToTop按钮的标签
        var goToTopButton = $('<div id="goToTop"><a href="javascript:;">点我回到页面顶部</a></div>');
        var thisTop = $(window).scrollTop() + topDistance;
        //在container的div里插入我们前面定义好的html标签元素
        $('.container').append(goToTopButton);
        //设置goToTop按钮top的css属性和属性值
        $('#goToTop').css('top' ,thisTop);
        if($(window).scrollTop() < showDistance){
            $('#goToTop').hide();
        }
        // 给窗口绑定一个滚动事件,当窗口滚动条滚动时执行
        $(window).scroll(function(){
            // console.log($(this).scrollTop());
            thisTop = $(this).scrollTop() + topDistance;        //获取当前window向上滚动的距离
            $('#goToTop').css('top', thisTop);                    //修改goToTop按钮的top距离
            console.log(thisTop);
            if($(this).scrollTop() > showDistance){
                $('#goToTop').fadeIn();
            }else{
                $('#goToTop').fadeOut();
            }
        });
        // 给按钮绑定一个click事件,点击按钮时,返回顶部
        $('#goToTop a').click(function(){
            $('html ,body').animate({scrollTop: 0}, 300);
            return false;
        });
    });
    </script>
</body>
</html>

4、小型插件版

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>返回顶部</title>
    <script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
    <style type="text/css">
       .container { width:980px; margin:0 auto; height:auto; min-height:100%; position:relative; }
       .content { height: 2000px; border: 1px solid red; }
       #goToTop { position: fixed; bottom: 20px; right: 10%; }
      #goToTop a { background: none repeat scroll 0 0 #336699; border: 1px solid #CCCCCC; border-radius: 3px; -webkit-border-radius: 3px; color: #FF9966; font-size: 14px; padding: 5px; text-decoration: none; text-shadow: 0 1px 0 #999; -webkit-text-shadow: 0 1px 0 #999; }
     </style>
</head>
<body>
    <div>
        <div> 我是头部</div>
        <div>我是主内容,高度是2000px</div>
        <div>我是在最底部</div>
        <div id="goToTop"><a href="javascript:;">点我回到页面顶部</a></div>
    </div>
    <script>
    // 编写jQuery返回顶部插件
    jQuery.fn.goToTop = function(){
        
        // 判断如果窗口滚动距离小于0,隐藏按钮
        if($(window).scrollTop() < 0){
            $('#goToTop').hide();
        }
        // 窗口滚动时,判断当前窗口滚动距离
        $(window).scroll(function(){
            if($(this).scrollTop() > 1){
                $('#goToTop').fadeIn();
            }else{
                $('#goToTop').fadeOut();
            }
        });
        // 给这个按钮绑定一个click事件
        this.bind('click',function(){
            $('html ,body').animate({scrollTop: 0},500);
            return false;
        });
    };
    //调用这个插件
    $(function(){
        $('#goToTop').goToTop();
    });
    </script>
</body>
</html>
相关推荐
  • jquery教程
  • 返回顶部
  • jquery方法
  • jQuery获取file控件中图片的宽高与大小

    jQuery获取file宽高的代码如下,仅在火狐中测试了,其他浏览器兼容性未知。var _URL = window.URL || window.webkitURL;$("#file").change(function (e) { var file, img; if ((file = this.files[0])) { img = new Image(); img.onload = func...

    js教程 117 4年前
  • Jquery实战视频教程 6小时精通Jq
    Jquery实战视频教程 6小时精通Jq

    Jquery实战视频教程 6小时精通Jq,实现小应用,附带教程源码。视频教程列表第7章 自动以Alert第6章 标记完成状态、定时提醒第5章 Task详情第4章 添加及查看Task第3章 细节完善第2章 整体布局第1章 ...

    视频教程 107 4年前
  • JQuery鼠标移动添加删除样式

    JQuery鼠标移动添加删除样式方法,下面为代码案列$("a").hover(function(){ $(this).find("b").show();},function(){ $(this).find("b").hide();})鼠标移到A标签触发事件,下面的B标签显示,后门的function就是鼠标移开隐藏。...

    js教程 72 3年前
  • jquery返回网站顶部方法大全

    1、基础版,只带返回顶部功能<!doctype html><html><head> <meta charset="UTF-8"> <title>返回顶部</title> <script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script> <style type=&...

    js教程 21 1年前
  • jquery返回网站顶部缓冲效果

    首先来段返回顶部按钮的HTML代码<div id="goToTop"><a href="javascript:;">点我返回顶部</a></div>然后就给按钮一个CSS样式,让它浮动在网页右下角,固定住。#goToTop { position: fixed; bottom: 20px;right: 10%; }最后就是JS代码了,记得要加载jquery...

    js教程 44 1年前
  • 双击页面任意空白处返回顶部JS代码

    双击页面任意空白处返回顶部JS代码,在所有页面空白处双击左键,自动返回网页顶部。(function() { &#39;use strict&#39;; // 定义一个变量用来记录上次点击的时间 var lastClickTime = 0; // 监听页面的点击事件 document.addEventLis...

    js教程 38 9个月前
  • jquery获取第几个索引方法

    使用jquery时经常会遇到,选择器选择一组元素后,需要在这组元素中找到第几个元素。jquery中使用eq()方法找到第几个元素或第N个元素,jquery中eq()的使用如下:eq() 选择器选取带有指定 index 值的元素。index 值从 0 开始,所有第一个元素的 index 值是 0(不...

    js教程 14 1年前
  • jquery控制radio单选框勾选和取消勾选方法

    attr()方法勾选$("input[type=&#39;radio&#39;]").attr("checked",&#39;checked&#39;);取消勾选$("input[type=&#39;radio&#39;]").removeAttr(&#39;checked&#39;);prop()方法勾选$("input[type=&#39;radio&#...

    js教程 37 1年前
  • jquery返回网站顶部方法大全

    1、基础版,只带返回顶部功能<!doctype html><html><head> <meta charset="UTF-8"> <title>返回顶部</title> <script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script> <style type=&...

    js教程 21 1年前
最新更新
  • js截取字符串教程

    slice()方法接受两个参数,起始索引和结束索引(可选)。它返回从起始索引到结束索引(不包括结束索引)之间的子字符...

    js教程 1个月前
  • find findIndex indexOf索引选择器使用方法

    find使用方法find方法是ES6引入的一种数组方法,可以用来查找数组中符合条件的元素。语法是:array.find(callba...

    js教程 2个月前
  • js复制网页内容教程

    Async Clipboard API方法HTML5新增的方法,无需引入第三方插件,直接就可以复制内容。低版本的浏览器可能会不兼...

    js教程 2个月前
  • js获取字符长度函数分享

    js获取字符长度函数function objLen(str) { if (str == null) return 0; if (typeof str != "string") { ...

    js教程 2个月前
  • 网站LED跑马灯效果广告代码

    网站可以看到很多的论坛网站都会用到这种网站LED跑马灯效果,这种效果实现也很简单,分享给大家。LED跑马灯效果...

    js教程 3个月前