我们在做网站的时间,经常会遇到返回顶部的效果,鼠标滚动一定高度之后,会出现“返回顶部”的标签。“返回顶部”标签被固定到窗口的指定位置,位置始终不变。当滚动的距离高度小于指定高度后,该“返回顶部”的标签消失。另外,“返回顶部标签”绑定点击事件,通过鼠标点击,实现文档回到顶部的效果。

直接放js就可以显示
111.JPG

  1. (function() {
  2.     var btnId = '__gotop';
  3.     var isIE = !!window.ActiveXObject && /msie (\d)/i.test(navigator.userAgent) ? RegExp['$1'] : false;

  4.     function $() {
  5.         return document.getElementById(arguments[0]);
  6.     }

  7.     function getScrollTop() {
  8.         return ('pageYOffset' in window) ? window.pageYOffset
  9.             : document.compatMode === "BackCompat"
  10.             && document.body.scrollTop
  11.             || document.documentElement.scrollTop ;
  12.     }   

  13.     function bindEvent(event, func) {
  14.         if (window.addEventListener) {
  15.             window.addEventListener(event, func, false);
  16.         } else if (window.attachEvent) {
  17.             window.attachEvent('on' + event, func);
  18.         }
  19.     }

  20.     bindEvent('load',
  21.         function() {
  22.             var css = 'background-color:#999;width:50px;height:50px;position:fixed;right:20px;bottom:30px;border-radius:10px;cursor:pointer;display:none;';

  23.             if (isIE && isIE < 7) {
  24.                 css += '_position:absolute;_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-30-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)))';
  25.                 var style = document.createStyleSheet();
  26.                 style.cssText = '*html{background-image:url(about:blank);background-attachment:fixed;}';
  27.             }

  28.             var html = '<div style="height: 0;width: 0;border:14px solid #999999;border-top: 0 none;border-bottom:14px solid #fff;position: relative;margin:12px 0 0 11px;"><div style="width:8px;height:7px;position:absolute;top:14px;left:-4px;background-color:#fff;overflow: hidden;"></div></div>';
  29.             var el = document.createElement('DIV');
  30.             el.id = btnId;
  31.             el.style.cssText = css;
  32.             el.innerHTML = html;
  33.             document.body.appendChild(el);

  34.             el.onclick = function() {
  35.                 (function() {
  36.                     var top = getScrollTop();
  37.                     if (top > 0) {
  38.                         window.scrollTo(0, top / 1.2)
  39.                         setTimeout(arguments.callee, 10);
  40.                     }
  41.                 })();
  42.             };

  43.             el.onmouseover = function() {
  44.                 $(btnId).firstChild.style.borderBottom = '14px solid #ddd';
  45.                 $(btnId).firstChild.firstChild.style.backgroundColor = '#ddd';
  46.             };

  47.             el.onmouseout = function() {
  48.                 $(btnId).firstChild.style.borderBottom = '14px solid #fff';
  49.                 $(btnId).firstChild.firstChild.style.backgroundColor = '#fff';
  50.             };
  51.         }
  52.     );

  53.     bindEvent('scroll',
  54.         function() {
  55.             var top = getScrollTop(), display = 'none';

  56.             if (top > 0) {
  57.                 display = 'block';
  58.             }

  59.             if ($(btnId)) $(btnId).style.display = display;
  60.         });
  61. })();
复制代码







上一篇:分享一个好用的二维码API有关的文章生成代码
下一篇:问题:The MySQL server is running with the --skip-grant-tables option
如无回复请发邮件