回到页面顶部的jquery代码

回到页面顶部的jquery代码

页面下滚后出现用于"回到页面顶部"按钮的效果, 感觉挺不错的, 所以自己也写了一个, 用jQuery写是再简单不过了. 下面就直接给出代码了

 .scroll-up {
  background: #dcdcdc url('up.png') no-repeat center center;
  width:48px !important;  /*for ff and other standard browser*/
  height:48px !important;
  _width: 58px;  /*for IE6 nonstandard box model*/
  _height: 58px;
  position: fixed;
  _position: absolute; /*for IE6 fixed bug*/
  opacity: .6;
     filter: Alpha(opacity=60); /*for IE opacity*/
  padding:5px;
  cursor: pointer;
  display: none;
 
  /*css教程3 property for ff chrome*/
  border-radius:5px;
  -webkit-transition-property: background-color, opacity;
  -webkit-transition-duration: 1s;
  -webkit-transition-timing-function: ease;
  
  -moz-transition-property: background-color;
  -moz-transition-duration: 1s;
  -moz-transition-timing-function: ease;
 }
 .scroll-up:hover {
  background-color:#B9B9B9;
  opacity: .8;
 }
 下面是jquery代码

  ;(function($) {
   $.scrollBtn = function(options) {
    var opts = $.extend({}, $.scrollBtn.defaults, options);

    var $scrollBtn = $('').css({
         bottom: opts.bottom + 'px',
         right: opts.right + 'px'
        }).addClass('scroll-up').attr('title', opts.title)
         .click(function() {
         $('html, body').animate({scrollTop: 0}, opts.duration);
        }).appendTo('body');
    
    // 绑定到window的scroll事件                 
    $(window).bind('scroll', function() {
     var scrollTop = $(document).scrollTop(),
      viewHeight = $(window).height();
     
     // 小于配置的显示范围 则fadeOut
     if(scrollTop <= opts.showScale) {
      
      if($scrollBtn.is(':visible'))
       $scrollBtn.fadeOut(500);
     
     // 大于配置的显示范围 则fadeIn
     } else {
     
      if($scrollBtn.is(':hidden'))
       $scrollBtn.fadeIn(500);
     }
     
     // 解决IE6下css中fixed没有效果的bug
     if(isIE6()) {
      var top = viewHeight + scrollTop - $scrollBtn.outerHeight() - opts.bottom;
      $scrollBtn.css('top', top + 'px');
     }
    });
    
    // 判断是否为IE6
    function isIE6() {
     if($.browser.msie) {
      if($.browser.version == '6.0') return true;
     }
    }
   };

   /**
    * -params
    *  -showScale: scroll down how much to show the scrollup button
    *  -right: to right of scrollable container
    *  -bottom: to bottom of scrollable container
    */
   $.scrollBtn.defaults = {  // 默认值
    showScale: 100,       // 超过100px 显示按钮
    right:10,            
    bottom:10,
    duration:200,         // 回到页面顶部的时间间隔
    title:'back to top'   // div的title属性
   }
  })(jQuery);
  
  $.scrollBtn({
   showScale: 200, //下滚200px后 显示按钮
   bottom:20, // 靠底部20px
   right:20   // 靠右部20px
  });

时间: 2024-07-30 10:58:05

回到页面顶部的jquery代码的相关文章

JQuery点击事件回到页面顶部效果的实现代码_jquery

JQuery点击事件回到页面顶部效果的实现代码 //2个div,点击某个时回到顶部 <div style="height:1000px">111111111111111</div> <div id="top" >top</div> <引用JQuery> <script type="text/javascript"> $(function(){ $("#top&quo

基于jquery的回到页面顶部按钮_jquery

css代码: 复制代码 代码如下: .scroll-up { background: #dcdcdc url('up.png') no-repeat center center; width:48px !important; /*for ff and other standard browser*/ height:48px !important; _width: 58px; /*for IE6 nonstandard box model*/ _height: 58px; position: fi

JS返回页面顶部实现程序代码

方法一,纯css实现方法 页面顶部放置:  代码如下 复制代码 <a name="top" id="top"></a> 放置位置在<body>标签之后随便找个地方放都可以,只要靠近顶部即可. 页面底部放置:  代码如下 复制代码 <a href="#top" target="_self">返回顶部</a> 方法二,js带css实现方法 本方式是渐进式返回顶部,要好看一

JS实现回到页面顶部动画效果的简单实例_javascript技巧

最近在模仿各大网站写页面样式和交互,发现好多都有回到顶部的需要,所以写了一下js,记录下来. 发现还可以添加从快到慢的动画效果和随时下拉滚动条停止滚动的功能, 参考了imooc上相关课程,最终实现JS代码如下: //页面加载后触发 window.onload = function(){ var btn = document.getElementById('btn'); var timer = null; var isTop = true; //获取页面可视区高度 var clientHeight

基于JS实现回到页面顶部的五种写法(从实现到增强)_javascript技巧

写法 [1]锚点 使用锚点链接是一种简单的返回顶部的功能实现.该实现主要在页面顶部放置一个指定名称的锚点链接,然后在页面下方放置一个返回到该锚点的链接,用户点击该链接即可返回到该锚点所在的顶部位置 [注意]关于锚点的详细信息移步至此 <body style="height:2000px;"> <div id="topAnchor"></div> <a href="#topAnchor" style=&qu

基于JS实现回到页面顶部的五种写法(从实现到增强)

这篇文章主要介绍了基于JS实现回到页面顶部的五种写法(从实现到增强)的相关资料,本文介绍的非常详细,实用性也非常高,非常具有参考借鉴价值,需要的朋友可以参考下   写法 [1]锚点 使用锚点链接是一种简单的返回顶部的功能实现.该实现主要在页面顶部放置一个指定名称的锚点链接,然后在页面下方放置一个返回到该锚点的链接,用户点击该链接即可返回到该锚点所在的顶部位置 [注意]关于锚点的详细信息移步至此 ? 1 2 3 4 <body style="height:2000px;">

回到页面顶部的方法总结

一.使用锚点链接 毫无疑问,使用锚点链接是一种简单的返回顶部的功能实现.该实现主要在页面顶部放置一个指定名称的锚点链接,然后我们在页面下方放置一个返回到该锚点的链接,用户点击该链接即可返回到该锚点所在的顶部位置. 示例代码如下: <!-- 定义一个名称为top的锚点链接 --> <a name="top"></a> <!-- 这里是网页主体内容,此处省略 --> <!-- 返回页面顶部top锚点的链接 --> <a hr

js控制的回到页面顶端goTop的代码实现_javascript技巧

有没有见过很多在页面的右下角有个[回到顶端]的悬浮东东,并且在开始时没有,一移动滚动条就出现,回到了顶端又消失的样子. 像: 控制的js代码如下: 复制代码 代码如下: function goTop(){ var _btn = document.getElementById("goTop"); if (document.documentElement && document.documentElement.scrollTop) { var _con = document

jQuery实现带有动画效果的回到顶部和底部代码_jquery

本文实例讲述了jQuery实现带有动画效果的回到顶部和底部代码.分享给大家供大家参考,具体如下: 这款动画版的回到顶部和底部效果代码,也算是比较常见的一款网页特效了,像淘宝网就有这种效果,使用了jQuery插件,加入了动画效果. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/jquery-animate-style-scroll-top-buttom-codes/ 具体代码如下: <!DOCTYPE html PUBLIC "-//W3C