问题描述:

在网页中,我们经常会由于网页内容过长,而需要在浏览网页时有一个快速回到网页顶部的功能,在浏览网页内容离顶部有一段距离时,出现快速回到网页顶部的工具,从而能使我们的网页更人性化。

问题的产生:

       前几天由于在公司做了个小项目中正好有一个这样的功能,当时也是以前同事用kissy框架写的组件,感觉蛮好用的,所以想能不能用Jquery把他们封装起来 这样以后如果离开了阿里系,那么就不可能用kissy框架 肯定大部分是用jquery框架了 ,所以.......  但是在这个代码之前在谷歌游览器下有个小小的bug。我想知道滚动top时候 用document.documentElement.scrollTop; 既然计算不到准确的值,经过仔细分析,发现Chrome对document.documentElement.scrollTop的识别会出现误差。不过加上document.body.scrollTop后,则显示正常。由于document.documentElement.scrollTop和document.body.scrollTop在标准模式或者是奇怪模式下都只有一个会返回有效的值,所以都加上也不会有问题(看来上面的问题是Chrome可能把文档当作非标准文档来解析了)。

代码如下 由于有详细的注释 所以可以直接看注释。

/**
 * Setting:
 * !!! css中需设置{width:**;bottom:**;},否则后果自负,哈哈
 *
 * 默认参数:
        topLink:'.top', //toplink
        markupType: 0, //默认为0:居中,1:居左,2:居右
        contentWidth: 1000, //布局宽度
        paddingWidth: 5, //仅当居右(左)时生效,表示距布局右(左)侧的间隔;
        leftOffset: 0, //仅当居中时生效,divbar左侧 需要超出布局的宽度
        //left和right 仅在markupType为-1 和 -2时有用。表示距窗口最左和最右的间隔
        left:0,
        right:0,
        //!!!top和bottom 必须设置其一 默认为top:0,顶对齐。
        YAlign: {
            top:0
        },
        zIndex:99999,//z-index属性
        display: 0//默认首屏不显示,为1则首屏显示。

//水平居中,顶部(间隔用YAlign: {top:0}设置,默认为0)
        new ActiveTool('#divbar',{
            topLink:'.top',//class or id
            markupType: 0
        });
        //水平居中,底部(间隔用YAlign: {bottom:'10px'}设置)
        new ActiveTool('#divbar',{
            topLink:'.top',//class or id
            markupType: 0,
            YAlign: {
                bottom:0
            }
        });
        //水平居左靠近主体左侧,顶部(间隔用YAlign: {top:0}设置)
        new ActiveTool('#divbar',{
            topLink:'.top',//class or id
            markupType: 1
        });
        //水平居左靠近主体左侧,底部(间隔用YAlign: {bottom:'10px'}设置)
        new ActiveTool('#divbar',{
            topLink:'.top',//class or id
            markupType: 1,
            YAlign: {
                bottom:0
            }
        });
        //水平居左,顶部(间隔用YAlign: {top:0}设置),左侧距离用style
        new ActiveTool('#divbar',{
            topLink:'.top',//class or id
            markupType: -1,
            left:'5px'
        });
        //水平居左,底部(间隔用YAlign: {bottom:'10px'}设置),左侧距离用left
        new ActiveTool('#divbar',{
            topLink:'.top',//class or id
            markupType: -1,
            left:'5px',
            YAlign: {
                bottom:0
            }
        });

****水平居右与水平居左类似****

* @change log:
 * 2013-8-20 tugenhua@126.com
 *
 */
var ActiveTool = (function(win,undefined){
    var doc = document,
        docBody = doc.body,
        isIE = navigator.userAgent.match(/MSIE/)!= null,
        isIE6 = navigator.userAgent.match(/MSIE 6.0/)!= null,
        docelem = doc.documentElement || docBody;

function ActiveTool(container,config){
        var self = this;
        if(!(self instanceof ActiveTool)){
            return new ActiveTool(container,config);
        }
        config = $.extend(ActiveTool.Config,config);

self.config = config || {};
        // 匹配传参container
        if($.isPlainObject(container)){
            self.container = container;
        }else if(/^\./.test(container)){
            self.container = $(container);
        }else if(/^#/.test(container)){
            self.container = $(container);
        }else if($('.'+container)){
            self.container = $('.'+container);
        }else{
            throw new Error('点击元素传递参数不符合!');
        }
        self._init();
    }
    
    // 默认配置

ActiveTool.Config = {
        topLink: '.top',
        markupType: 0,
        contentWidth: 1000,
        paddingWidth: 5,
        leftOffset: 0,
        left:0,
        right:0,
        YAlign: {
            top:0
        },
        zIndex:99999,
        display: 0    
    };
    ActiveTool.prototype = {
        _init: function(){
            var self = this,
                cfg = self.config,
                EVT_SCROLL = 'scroll',
                EVT_RESIZE = 'resize',
                divBar;
            // init divBar node
            divBar = self.container;
            
            // init position
            self._calcPos();

// 首屏是否显示
            if(cfg.display === 0){
                $(divBar).css({"opacity":0})
                $(divBar).css('zIndex',-1);
            }else{
                $(divBar).css({"opacity":1});
                $(divBar).css('zIndex',cfg.zIndex);

}

var topLink = $(cfg.topLink,divBar);
            if (topLink){
                // hide focus for ie
                if (isIE) {
                    topLink.hideFocus = true;
                }
                $(topLink).bind('click',function(e){
                    e.preventDefault();
                    $(topLink).unbind('click');
                    docelem.scrollTop = 0;
                });
            }
            
            // do decoration on scrolling
            
            $(win).bind(EVT_SCROLL,function(e){
                self._decorate(true);
            });

$(win).bind(EVT_RESIZE,function(e){
                self._decorate(false);
            });
        },
        //计算各浏览下的定位坐标
        _calcPos: function(){
            var self = this,
                cfg = self.config,
                PX = 'px',
                TOP = 'top',
                BOTTOM = 'bottom',
                LEFT = 'left',
                RIGHT = 'right',
                contentWidth = cfg.contentWidth,
                paddingWidth = cfg.paddingWidth,
                leftOffset = cfg.leftOffset,
                divBar = self.container,
                top = cfg.YAlign.top,
                bottomPadding = parseInt(cfg.YAlign.bottom,10),
                topPadding = parseInt(top,10),
                selfHeight = parseInt($(divBar).css('height'),10),//divbar自身高度
                selfWidth = parseInt($(divBar).css('width'),10),//divbar自身宽度
                xPos,
                checkMarkup = function(viewWidth){
                    switch (true){

case cfg.markupType === 0:
                            xPos = Math.floor((viewWidth - contentWidth) / 2) - leftOffset;
                            break;

case cfg.markupType === 2:
                            xPos = Math.floor((viewWidth - contentWidth) / 2) + contentWidth + paddingWidth;
                            break;

case cfg.markupType === -2:
                            xPos = parseInt(cfg.right,10);
                            $(divBar).css(RIGHT, xPos + PX);
                            break;

case cfg.markupType === 1:
                            xPos = Math.floor((viewWidth - contentWidth) / 2) - paddingWidth - selfWidth;
                            break;

case cfg.markupType === -1:
                            xPos = parseInt(cfg.left,10);
                            break;

default:
                            xPos = Math.floor((viewWidth - contentWidth) / 2) - leftOffset;
                    }
                },
                cal;
            if (true === isIE6) {
                // for IE6
                cal = function(scrolling){
                    var viewHeight = $(win).height(),
                        scrollTop = docelem.scrollTop,
                        yPos;

if (top !== undefined){
                        yPos = topPadding + scrollTop;
                    } else {
                        yPos = viewHeight - bottomPadding - selfHeight + scrollTop;
                    }

// on scrolling
                    if (scrolling) {
                        DOM.css(divBar, TOP, yPos + PX);
                        return;
                    }

var viewWidth = $(win).width();
                    checkMarkup(viewWidth);
                    $(divBar).css({'position':'absolute'});
                    $(divBar).css(TOP, yPos + PX);
                    if (cfg.markupType === -2){return;}
                    $(divBar).css(LEFT, xPos + PX);
                };
            } else if (isIE) {
                // for IE7+ (ie9有待验证)
                cal = function () {
                    $(divBar).css({'position':'fixed'});
                    top !== undefined ? $(divBar).css(TOP, topPadding + PX) : $(divBar).css(BOTTOM, bottomPadding + PX);
                    var viewWidth = $(win).width();
                    checkMarkup(viewWidth);
                    if (cfg.markupType === -2){return;}
                    $(divBar).css(LEFT, xPos + PX);
                };
            } else {
                // for non-IE
                cal = function () {
                    
                    $(divBar).css({'position':'fixed'});
                    top !== undefined ? $(divBar).css(TOP, topPadding + PX) : $(divBar).css(BOTTOM, bottomPadding + PX);
                    var viewWidth = document.body.clientWidth;
                    checkMarkup(viewWidth);
                    if (cfg.markupType === -2){return;}
                    $(divBar).css(LEFT, xPos + PX);
                };
            }
            self._calcPos = cal;
            return cal();
        },
        /* win 注册 scroll resize 事件
         * @param flag -> true 指滚动 flag -> false 指缩放
         */
        _decorate: function(flag){
            var self = this,
                cfg = self.config,
                divBar = self.container,
                DELAY = 100,
                scrollTimer,
                resizeTimer;
            if(flag){
                var scrollTop = document.documentElement.scrollTop + document.body.scrollTop;
                if(0 === scrollTop && cfg.display === 0){
                    $(divBar).css({'opacity':0});
                    $(divBar).css('zIndex', -1);
                }else {
                    $(divBar).css({'opacity':1});
                    $(divBar).css('zIndex', cfg.zIndex);
                    scrollTimer && clearTimeout(scrollTimer);
                    scrollTimer = setTimeout(function(){
                        self._calcPos();
                    },DELAY);
                }
            }else{
                resizeTimer && clearTimeout(resizeTimer);
                resizeTimer = setTimeout(function(){
                    self._calcPos();
                },DELAY);
            }
        }
    };
    return ActiveTool;

})(window);

Jquery回到顶部功能的更多相关文章

  1. js实现回到顶部功能

    js实现回到顶部功能 一.总结 一句话总结: 可以通过js或者jquery可以很快的控制页面的属性,比如高度等等 //设置当前视口的顶端数值 var setScrollTop = function(t ...

  2. jQuery回到顶部

    jquery回到顶部 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=& ...

  3. jQuerry点击按钮回到顶部功能

    简单实现点击按钮回到顶部功能

  4. jquery javascript 回到顶部功能

    今天搞了一个回到顶部的JS JQ功能 (function($){ $.fn.survey=function(options){ var defaults={width:"298", ...

  5. jQuery实现页面回到顶部功能

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  6. html css jquery 回到顶部按钮

    今天做了个回到顶部的功能,在这里记录一下,有需要可以拿去试试! CSS部分,很简单就一个class /*回到顶部*/ .back-top { position: fixed; right: 15px; ...

  7. 通过js实现回到顶部功能

    许多商城网址,当我们滚动到一定高度时,我们会发现一般会出现一个回到顶部的js选项,点击轻松实现回到顶部,交互效果会显得比较人性化,且回到顶部过程中若在滚动滚动条时可以停止滚动,现在让我们来实现吧 我总 ...

  8. 【JavaScript Demo】回到顶部功能实现

    随着网站的不断发展,需要展示的内容也越来越丰富,这导致网页上能展示的内容越来越多.当内容堆积影响了用户体验,就需考虑如何提升用户体验.在这一系列的改动中,“回到顶部”的功能成为了一个经典. 1.页面布 ...

  9. Jquery 回到顶部

    转:http://www.cnblogs.com/DemoLee/archive/2012/04/20/2459082.html 用jQuery实现渐隐渐显的返回顶部效果(附多图)   先来看几个图片 ...

随机推荐

  1. 从实例角度分析java的public、protected、private和default访问权限

    一.public 同一个package 1.本类内部 public class A { public int f=1; public void m1() {} public void m2() { f ...

  2. Code Signal_练习题_alphabeticShift

    Given a string, replace each its character by the next one in the English alphabet (z would be repla ...

  3. JS 正则截取字符串

    1.js截取两个字符串之间的内容: varstr = "aaabbbcccdddeeefff"; str = str.match(/aaa(\S*)fff/)[1]; alert( ...

  4. 第三十四天- 线程队列、线程池(map/submit/shutdown/回调函数)

    1.线程列队 queue队列 :使用import queue,用法与进程Queue一样 class queue.Queue(maxsize=0) # 先进先出: q = queue.Queue(3) ...

  5. thymeleaf标签使用方法总结

    https://blog.csdn.net/quuqu/article/details/52511933 常用th标签https://www.cnblogs.com/suncj/p/4030393.h ...

  6. Zookeeper简单初应用

    一.Zookeeper 1.1 概述 Zookeeper是一个开源的分布式的,为分布式应用提供协调服务的Apache项目. Zookeeper从设计模式角度来理解:是一个基于观察者模式设计的分布式服务 ...

  7. Sql Server Express 2005提示"failed to generate a user instance of sql server "

    解决方法: 修改连接字符串“Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\mydb.mdf;Integrated Security ...

  8. Android IOC注解库EasyUI

    EasyUI介绍 1.使用反射机制和注解实现类似于butterknife的IOC框架 2.快速的findViewById和OnClick 3.扩展了click时无网络监测 4.扩展了快速点击监测 使用 ...

  9. 韩顺平php从入门到精通

    37 整型细节说明 $a; echo $a; var_dump($a) //NULL 一个数总是要占用内存空间(字节),在php中一个整数一般占用四个字节(与平台相关),一个字节占用8bit php的 ...

  10. eclipse中svn插件的工程不能与svn资源库同步的解决方法

    eclipse中svn插件的工程不能与svn资源库同步的解决办法 最近几天自己的工程与资源库同步总是出现问题,重启机器后发现资源库丢失了,无法进行同步. 解决办法如下: 1.右键工程---->选 ...