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 相关文章推荐
html读出文本文件内容
Jan 22 Javascript
jQuery EasyUI 开源插件套装 完全替代ExtJS
Mar 24 Javascript
jquery实现类似淘宝星星评分功能实例
Sep 12 Javascript
director.js实现前端路由使用实例
Feb 03 Javascript
animate 实现滑动切换效果【实例代码】
May 05 Javascript
Javascript实现图片加载从模糊到清晰显示的方法
Jun 21 Javascript
浅谈javascript alert和confirm的美化
Dec 15 Javascript
jQuery实现的粘性滚动导航栏效果实例【附源码下载】
Oct 19 jQuery
vue首次赋值不触发watch的解决方法
Sep 11 Javascript
js全屏事件fullscreenchange 实现全屏、退出全屏操作
Sep 17 Javascript
Vuejs中的watch实例详解(监听者)
Jan 05 Javascript
node脚手架搭建服务器实现token验证的方法
Jan 20 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处理json时中文问题的解决方法
2011/04/12 PHP
php使用PDO方法详解
2014/12/27 PHP
PHP数组函数知识汇总
2016/05/12 PHP
关于Yii中模型场景的一些简单介绍
2019/09/22 PHP
网页加载时页面显示进度条加载完成之后显示网页内容
2012/12/23 Javascript
JavaScript获取flash对象与网上的有所不同
2014/04/21 Javascript
js获取input长度并根据页面宽度设置其大小及居中对齐
2014/08/22 Javascript
JS取得绝对路径的实现代码
2015/01/16 Javascript
JQuery 的跨域方法推荐_可跨任何网站
2016/05/18 Javascript
Angular将填入表单的数据渲染到表格的方法
2017/09/22 Javascript
JavaScript中常见内置函数用法示例
2018/05/14 Javascript
AngularJS自定义过滤器用法经典实例总结
2018/05/17 Javascript
vue中vee validate表单校验的几种基本使用
2018/06/25 Javascript
JavaScript常见JSON操作实例分析
2018/08/08 Javascript
vuejs+element UI点击编辑表格某一行时获取内容填入表单的示例
2018/10/31 Javascript
ant-design-vue中的select选择器,对输入值的进行筛选操作
2020/10/24 Javascript
在antd Form表单中select设置初始值操作
2020/11/02 Javascript
pycharm+PyQt5+python最新开发环境配置(踩坑)
2019/02/11 Python
pytorch 模型可视化的例子
2019/08/17 Python
pytorch cuda上tensor的定义 以及减少cpu的操作详解
2020/06/23 Python
使用HTML5 Canvas为图片填充颜色和纹理的教程
2016/03/21 HTML / CSS
华润集团网上药店:健一网
2016/09/19 全球购物
FOREO斐珞尔官方旗舰店:LUNA露娜洁面仪
2018/03/11 全球购物
Belvilla德国:在线预订度假屋
2018/04/10 全球购物
荷兰音乐会和音乐剧门票订购网站:Topticketshop
2019/08/27 全球购物
包装类的功能、种类、常用方法
2012/01/27 面试题
介绍一下Python下range()函数的用法
2013/11/07 面试题
电气自动化大学生求职信
2013/10/16 职场文书
高中班主任评语
2014/12/30 职场文书
党员带头倡议书
2015/04/29 职场文书
图书馆义工感想
2015/08/07 职场文书
严以用权专题学习研讨会发言材料
2015/11/09 职场文书
如何让2019年上半年的工作总结更出色!
2019/07/01 职场文书
实习报告范文之电话客服岗位
2019/07/26 职场文书
导游词之南昌滕王阁
2019/11/29 职场文书
Python字符串的转义字符
2022/04/07 Python