首頁 web前端 js教程 JQuery 实现的页面滚动时浮动窗口控件_jquery

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

May 16, 2016 pm 06:50 PM
jquery 頁面捲動

JQuery 实现的页面滚动时浮动窗口控件_jquery
JQuery 实现的页面滚动时浮动窗口控件_jquery
1. Introduction:
这个控件能够实现的效果是当你的页面滚动时,某个DIV永远停留在你需要它停留的位置。同时可以为这个DIV设定个容器,当滚动条已经超过了这个容器,那么这个DIV就不再滚动了。
JQuery 实现的页面滚动时浮动窗口控件_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 实现的页面滚动时浮动窗口控件_jquery
上面图示是用来设定这个DIV在滚动后的位置会在哪里。
而所有的动画效果参数设置如下:
JQuery 实现的页面滚动时浮动窗口控件_jquery
那么如何在HTML或者是其它的页面中使用呢?
复制代码 代码如下:


 
最后是设置ID为example这个DIV的Css样式,需要注意的是position必须设定为relative,如下例:
复制代码 代码如下:

#example {
position: relative;
width: 220px;
margin: 5px;
padding: 10px;
background: #DDDDDD;
border: 1px solid #42CBDC;
}
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

jQuery引用方法詳解:快速上手指南 jQuery引用方法詳解:快速上手指南 Feb 27, 2024 pm 06:45 PM

jQuery引用方法詳解:快速上手指南jQuery是一個受歡迎的JavaScript庫,被廣泛用於網站開發中,它簡化了JavaScript編程,並為開發者提供了豐富的功能和特性。本文將詳細介紹jQuery的引用方法,並提供具體的程式碼範例,幫助讀者快速上手。引入jQuery首先,我們需要在HTML檔案中引入jQuery函式庫。可以透過CDN連結的方式引入,也可以下載

jQuery中如何使用PUT請求方式? jQuery中如何使用PUT請求方式? Feb 28, 2024 pm 03:12 PM

jQuery中如何使用PUT請求方式?在jQuery中,發送PUT請求的方法與發送其他類型的請求類似,但需要注意一些細節和參數設定。 PUT請求通常用於更新資源,例如更新資料庫中的資料或更新伺服器上的檔案。以下是在jQuery中使用PUT請求方式的具體程式碼範例。首先,確保引入了jQuery庫文件,然後可以透過以下方式發送PUT請求:$.ajax({u

jQuery如何移除元素的height屬性? jQuery如何移除元素的height屬性? Feb 28, 2024 am 08:39 AM

jQuery如何移除元素的height屬性?在前端開發中,經常會遇到需要操作元素的高度屬性的需求。有時候,我們可能需要動態改變元素的高度,而有時候又需要移除元素的高度屬性。本文將介紹如何使用jQuery來移除元素的高度屬性,並提供具體的程式碼範例。在使用jQuery操作高度屬性之前,我們首先需要了解CSS中的height屬性。 height屬性用於設定元素的高度

深度剖析:jQuery的優勢與劣勢 深度剖析:jQuery的優勢與劣勢 Feb 27, 2024 pm 05:18 PM

jQuery是一款廣泛應用於前端開發的快速、小巧、功能豐富的JavaScript庫。自2006年發布以來,jQuery已成為眾多開發者的首選工具之一,但在實際應用中,它也不乏一些優點和缺點。本文將深度剖析jQuery的優勢與劣勢,並結合具體的程式碼範例進行說明。優點:1.簡潔的語法jQuery的語法設計簡潔明了,可以大幅提升程式碼的可讀性和編寫效率。比如,

jQuery小技巧:快速修改頁面所有a標籤的文本 jQuery小技巧:快速修改頁面所有a標籤的文本 Feb 28, 2024 pm 09:06 PM

標題:jQuery小技巧:快速修改頁面所有a標籤的文字在網頁開發中,我們經常需要對頁面中的元素進行修改和操作。使用jQuery時,有時候需要一次修改頁面中所有a標籤的文字內容,這樣可以節省時間和精力。以下將介紹如何使用jQuery快速修改頁面所有a標籤的文本,同時給出具體的程式碼範例。首先,我們需要引入jQuery庫文件,確保在頁面中引入了以下程式碼:&lt

使用jQuery修改所有a標籤的文字內容 使用jQuery修改所有a標籤的文字內容 Feb 28, 2024 pm 05:42 PM

標題:使用jQuery修改所有a標籤的文字內容jQuery是一款受歡迎的JavaScript庫,被廣泛用於處理DOM操作。在網頁開發中,經常會遇到需要修改頁面上連結標籤(a標籤)的文字內容的需求。本文將介紹如何使用jQuery來實現這個目標,並提供具體的程式碼範例。首先,我們需要在頁面中引入jQuery庫。在HTML檔案中加入以下程式碼:

如何判斷jQuery元素是否具有特定屬性? 如何判斷jQuery元素是否具有特定屬性? Feb 29, 2024 am 09:03 AM

如何判斷jQuery元素是否具有特定屬性?在使用jQuery操作DOM元素時,常會遇到需要判斷元素是否具有某個特定屬性的情況。在這種情況下,我們可以藉助jQuery提供的方法來輕鬆實現這項功能。以下將介紹兩種常用的方法來判斷一個jQuery元素是否具有特定屬性,並附上具體的程式碼範例。方法一:使用attr()方法和typeof運算子//判斷元素是否具有特定屬

了解jQuery中eq的作用及應用場景 了解jQuery中eq的作用及應用場景 Feb 28, 2024 pm 01:15 PM

jQuery是一種流行的JavaScript庫,被廣泛用於處理網頁中的DOM操作和事件處理。在jQuery中,eq()方法是用來選擇指定索引位置的元素的方法,具體使用方法和應用場景如下。在jQuery中,eq()方法選擇指定索引位置的元素。索引位置從0開始計數,即第一個元素的索引是0,第二個元素的索引是1,依此類推。 eq()方法的語法如下:$("s

See all articles