JQuery 实现的页面滚动时浮动窗口控件


Posted in Javascript onJuly 10, 2009

JQuery 实现的页面滚动时浮动窗口控件
JQuery 实现的页面滚动时浮动窗口控件
1. Introduction:
这个控件能够实现的效果是当你的页面滚动时,某个DIV永远停留在你需要它停留的位置。同时可以为这个DIV设定个容器,当滚动条已经超过了这个容器,那么这个DIV就不再滚动了。
JQuery 实现的页面滚动时浮动窗口控件
有时候如果需要做个比较好用的导航条,使用这个控件挺不错的。
2. Code & Properties:
这个js文件是在jQuery和JQeury UI的核心上扩展的。所以使用它前你必须到JQuery的官网下载那两个js文件,jquery.js和ui.core.js。
整个javascript如下:

( function( $ ) {     $.scrollFollow = function ( box, options ) 
    { 
        // Convert box into a jQuery object 
        box = $( box ); 
        // 'box' is the object to be animated 
        var position = box.css( 'position' ); 
        function ani() 
        {         
            // The script runs on every scroll which really means many times during a scroll. 
            // We don't want multiple slides to queue up. 
            box.queue( [ ] ); 
            // A bunch of values we need to determine where to animate to 
            var viewportHeight = parseInt( $( window ).height() );     
            var pageScroll = parseInt( $( document ).scrollTop() ); 
            var parentTop = parseInt( box.cont.offset().top ); 
            var parentHeight = parseInt( box.cont.attr( 'offsetHeight' ) ); 
            var boxHeight = parseInt( box.attr( 'offsetHeight' ) + ( parseInt( box.css( 'marginTop' ) ) || 0 ) + ( parseInt( box.css( 'marginBottom' ) ) || 0 ) ); 
            var aniTop; 
            // Make sure the user wants the animation to happen 
            if ( isActive ) 
            { 
                // If the box should animate relative to the top of the window 
                if ( options.relativeTo == 'top' ) 
                { 
                    // Don't animate until the top of the window is close enough to the top of the box 
                    if ( box.initialOffsetTop >= ( pageScroll + options.offset ) ) 
                    { 
                        aniTop = box.initialTop; 
                    } 
                    else 
                    { 
                        aniTop = Math.min( ( Math.max( ( -parentTop ), ( pageScroll - box.initialOffsetTop + box.initialTop ) ) + options.offset ), ( parentHeight - boxHeight - box.paddingAdjustment ) ); 
                    } 
                } 
                // If the box should animate relative to the bottom of the window 
                else if ( options.relativeTo == 'bottom' ) 
                { 
                    // Don't animate until the bottom of the window is close enough to the bottom of the box 
                    if ( ( box.initialOffsetTop + boxHeight ) >= ( pageScroll + options.offset + viewportHeight ) ) 
                    { 
                        aniTop = box.initialTop; 
                    } 
                    else 
                    { 
                        aniTop = Math.min( ( pageScroll + viewportHeight - boxHeight - options.offset ), ( parentHeight - boxHeight ) ); 
                    } 
                } 
                // Checks to see if the relevant scroll was the last one 
                // "-20" is to account for inaccuracy in the timeout 
                if ( ( new Date().getTime() - box.lastScroll ) >= ( options.delay - 20 ) ) 
                { 
                    box.animate( 
                        { 
                            top: aniTop 
                        }, options.speed, options.easing 
                    ); 
                } 
            } 
        }; 
        // For user-initiated stopping of the slide 
        var isActive = true; 
        if ( $.cookie != undefined ) 
        { 
            if( $.cookie( 'scrollFollowSetting' + box.attr( 'id' ) ) == 'false' ) 
            { 
                var isActive = false; 
                $( '#' + options.killSwitch ).text( options.offText ) 
                    .toggle( 
                        function () 
                        { 
                            isActive = true; 
                            $( this ).text( options.onText ); 
                            $.cookie( 'scrollFollowSetting' + box.attr( 'id' ), true, { expires: 365, path: '/'} ); 
                            ani(); 
                        }, 
                        function () 
                        { 
                            isActive = false; 
                            $( this ).text( options.offText ); 
                            box.animate( 
                                { 
                                    top: box.initialTop 
                                }, options.speed, options.easing 
                            );     
                            $.cookie( 'scrollFollowSetting' + box.attr( 'id' ), false, { expires: 365, path: '/'} ); 
                        } 
                    ); 
            } 
            else 
            { 
                $( '#' + options.killSwitch ).text( options.onText ) 
                    .toggle( 
                        function () 
                        { 
                            isActive = false; 
                            $( this ).text( options.offText ); 
                            box.animate( 
                                { 
                                    top: box.initialTop 
                                }, 0 
                            );     
                            $.cookie( 'scrollFollowSetting' + box.attr( 'id' ), false, { expires: 365, path: '/'} ); 
                        }, 
                        function () 
                        { 
                            isActive = true; 
                            $( this ).text( options.onText ); 
                            $.cookie( 'scrollFollowSetting' + box.attr( 'id' ), true, { expires: 365, path: '/'} ); 
                            ani(); 
                        } 
                    ); 
            } 
        } 
        // If no parent ID was specified, and the immediate parent does not have an ID 
        // options.container will be undefined. So we need to figure out the parent element. 
        if ( options.container == '') 
        { 
            box.cont = box.parent(); 
        } 
        else 
        { 
            box.cont = $( '#' + options.container ); 
        } 
        // Finds the default positioning of the box. 
        box.initialOffsetTop = parseInt( box.offset().top ); 
        box.initialTop = parseInt( box.css( 'top' ) ) || 0; 
        // Hack to fix different treatment of boxes positioned 'absolute' and 'relative' 
        if ( box.css( 'position' ) == 'relative' ) 
        { 
            box.paddingAdjustment = parseInt( box.cont.css( 'paddingTop' ) ) + parseInt( box.cont.css( 'paddingBottom' ) ); 
        } 
        else 
        { 
            box.paddingAdjustment = 0; 
        } 
        // Animate the box when the page is scrolled 
        $( window ).scroll( function () 
            { 
                // Sets up the delay of the animation 
                $.fn.scrollFollow.interval = setTimeout( function(){ ani();} , options.delay ); 
                // To check against right before setting the animation 
                box.lastScroll = new Date().getTime(); 
            } 
        ); 
        // Animate the box when the page is resized 
        $( window ).resize( function () 
            { 
                // Sets up the delay of the animation 
                $.fn.scrollFollow.interval = setTimeout( function(){ ani();} , options.delay ); 
                // To check against right before setting the animation 
                box.lastScroll = new Date().getTime(); 
            } 
        ); 
        // Run an initial animation on page load 
        box.lastScroll = 0; 
        ani(); 
    }; 
    $.fn.scrollFollow = function ( options ) 
    { 
        options = options || {}; 
        options.relativeTo = options.relativeTo || 'top'; 
        options.speed = options.speed || 1; 
        options.offset = options.offset || 0; 
        options.easing = options.easing || 'swing'; 
        options.container = options.container || this.parent().attr( 'id' ); 
        options.killSwitch = options.killSwitch || 'killSwitch'; 
        options.onText = options.onText || 'Turn Slide Off'; 
        options.offText = options.offText || 'Turn Slide On'; 
        options.delay = options.delay || 0; 
        this.each( function() 
            { 
                new $.scrollFollow( this, options ); 
            } 
        ); 
        return this; 
    }; 
})( jQuery );

这里面有几个参数可以设置效果:
JQuery 实现的页面滚动时浮动窗口控件
上面图示是用来设定这个DIV在滚动后的位置会在哪里。
而所有的动画效果参数设置如下:
JQuery 实现的页面滚动时浮动窗口控件
那么如何在HTML或者是其它的页面中使用呢?
<script type="text/javascript"><!-- 
$( document ).ready( function () 
{ 
$( '#example' ).scrollFollow(); 
} 
); 
// --></script>
 
最后是设置ID为example这个DIV的Css样式,需要注意的是position必须设定为relative,如下例:
#example { 
position: relative; 
width: 220px; 
margin: 5px; 
padding: 10px; 
background: #DDDDDD; 
border: 1px solid #42CBDC; 
}
Javascript 相关文章推荐
javascript innerText和innerHtml应用
Jan 28 Javascript
仿当当网淘宝网等主流电子商务网站商品分类导航菜单
Sep 25 Javascript
ECMAScript6新增值比较函数Object.is
Jun 12 Javascript
jquery中ready()函数执行的时机和window的load事件比较
Jun 22 Javascript
Node.js的Koa框架上手及MySQL操作指南
Jun 13 Javascript
原生JS实现幻灯片
Feb 22 Javascript
js 获取图像缩放后的实际宽高,位置等信息
Mar 07 Javascript
原生JS京东轮播图代码
Mar 22 Javascript
利用Angular2 + Ionic3开发IOS应用实例教程
Jan 15 Javascript
angular中如何绑定iframe中src的方法
Feb 01 Javascript
JavaScript实现栈结构Stack过程详解
Mar 07 Javascript
javascript实现页面的实时时钟显示示例
Aug 06 Javascript
javascript 读取xml,写入xml 实现代码
Jul 10 #Javascript
jquery 1.3.2 IE8中的一点点的小问题解决方法
Jul 10 #Javascript
jquery Firefox3.5中操作select的问题
Jul 10 #Javascript
jQuery 版本的文本输入框检查器Input Check
Jul 09 #Javascript
window.onload 加载完毕的问题及解决方案(下)
Jul 09 #Javascript
window.onload 加载完毕的问题及解决方案(上)
Jul 09 #Javascript
最简单的jQuery程序 入门者学习
Jul 09 #Javascript
You might like
PHP静态文件生成类实例
2014/11/29 PHP
php微信支付之APP支付方法
2015/03/04 PHP
php+mysql+ajax实现单表多字段多关键词查询的方法
2017/04/15 PHP
JS版网站风格切换实例代码
2008/10/06 Javascript
jQuery之end()和pushStack()使用介绍
2012/02/07 Javascript
html5的自定义data-*属性和jquery的data()方法的使用示例
2013/08/21 Javascript
纯JS实现动态时间显示代码
2014/02/08 Javascript
jquery序列化form表单使用ajax提交后处理返回的json数据
2014/03/03 Javascript
javaScript中push函数用法实例分析
2015/06/08 Javascript
利用Javascript实现简单的转盘抽奖
2017/02/13 Javascript
利用node.js如何搭建一个简易的即时响应服务器
2017/05/28 Javascript
微信小程序 监听手势滑动切换页面实例详解
2017/06/15 Javascript
Javascript中将变量转换为字符串的三种方法
2017/09/19 Javascript
微信小程序wx.getImageInfo()如何获取图片信息
2018/01/26 Javascript
在vue中读取本地Json文件的方法
2018/09/06 Javascript
解决IOS端微信H5页面软键盘弹起后页面下方留白的问题
2019/06/05 Javascript
javascript中的with语句学习笔记及用法
2020/02/17 Javascript
javascript设计模式 ? 状态模式原理与用法实例分析
2020/04/22 Javascript
浅谈vue单页面中有多个echarts图表时的公用代码写法
2020/07/19 Javascript
[45:40]Ti4 冒泡赛第二天NEWBEE vs NaVi 1
2014/07/15 DOTA
python字符串替换示例
2014/04/24 Python
关于ResNeXt网络的pytorch实现
2020/01/14 Python
tensorflow之变量初始化(tf.Variable)使用详解
2020/02/06 Python
python_matplotlib改变横坐标和纵坐标上的刻度(ticks)方式
2020/05/16 Python
有关HTML5页面在iPhoneX适配问题
2017/11/13 HTML / CSS
Lookfantastic香港官网:英国知名美妆购物网站
2018/06/19 全球购物
高中英语教学反思
2014/02/04 职场文书
高中物理教学反思
2014/02/08 职场文书
主管会计岗位责任制
2014/02/10 职场文书
北体毕业生求职信
2014/02/28 职场文书
珍惜时间演讲稿
2014/05/14 职场文书
2014学生会工作总结报告
2014/12/02 职场文书
优秀共青团员事迹材料
2014/12/25 职场文书
2015年维修工作总结
2015/04/25 职场文书
创作书写之导游词实用技巧分享(干货)
2019/12/20 职场文书
python文件名批量重命名脚本实例代码
2021/04/22 Python