• Js、Jquery对goTop功能的实现


    本文介绍用原生JS和Jquery分别实现的网页goTopbutton功能,以及在这个过程中碰到的问题。


    终于实现的效果类似:http://sc2.163.com/home(注意右下角的top)


    代码:

    Jquery

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
        <style>
            body{height: 2000px;}
            #goTop{
                display: none;
                cursor:pointer;
                background: url("common.png") no-repeat scroll 0 -460px;
                position: fixed;
                 60px;
                height: 60px;
                right: 20px;
                bottom: 20px;
                text-indent: -9999px;
                z-index: 100;
            }
            #goTop:hover{
                background-position: -100px -460px;
            }
        </style>
        <script src="../jQuery/jquery-1.11.3.js"></script>
        <script>
                $(function(){
                    $(window).scroll(function(){
                        var scrH=document.documentElement.scrollTop+document.body.scrollTop;
                        if(scrH>200){
                            $('#goTop').fadeIn(400);
                        }else{
                            $('#goTop').stop().fadeOut(400);
                        }
                    });
                    $('#goTop').click(function(){
                        $('html,body').animate({scrollTop:'0px'},200);
                    });
                });
        </script>
    </head>
    <body>
    <a id="goTop" href="javascript:">返回顶层</a>
    </body>
    </html>

    JS
    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
        <style>
            body{height: 2000px;}
            #goTop{
                display: none;
                cursor:pointer;
                background:url("common.png") no-repeat scroll 0 -460px;
                position: fixed;
                 60px;
                height: 60px;
                right: 20px;
                bottom: 20px;
                z-index: 100;
                text-indent: -9999px;
            }
            #goTop:hover{
                background-position: -100px -460px;
            }
        </style>
    </head>
    <body>
    <a id="goTop" title="返回顶部" href="javascript:scroll(0,0)">返回顶层</a>
    </body>
    </html>
    <script>
        (function() {
            function $(id){
                return document.getElementById(id);
            }
            window.onscroll=function(){
                var scrH=document.documentElement.scrollTop+document.body.scrollTop;
                if(scrH>200){
                    $('goTop').style.display='block';
                }else{
                    $('goTop').style.display='none';
                }
            };
        }());
    </script>

    代码好像没啥特别好解释了。说说我碰到的问题吧。

    ①在IE低版本号下。不支持rgba属性~

    ②原生js的动画效果还不会实现,希望有人留言教下。


  • 相关阅读:
    HDU4685 Prince and Princess 完美搭配+良好的沟通
    坚持 本身是一种策略
    PowerDesigner中SQL文件、数据库表反向生成PDM
    Filter技术+职责链模式
    [ACM] poj 1258 Agri-Net (最小生成树)
    android 屏幕适配 课程笔记
    HDU 5071 Chat
    【玩转微信公众平台之中的一个】序章(纯粹扯淡)
    HTML表格标签的使用-&lt;table&gt;
    hdu 1251 统计难题 (map水过)
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7147218.html
Copyright © 2020-2023  润新知