jQuery視差滾動外掛程式,(附原理分析,調用方法)

來源:互聯網
上載者:User

標籤:

示範地址:http://www.jq22.com/jquery-info1799

 

jquery外掛程式,使用驗證過可用。

分析原始碼後總結原理:設定background樣式為fixed,判斷瀏覽器滾動距離,當在視窗範圍內時,調用$(window).scroll。並根據所設定的速度比例,進行同步滾動。

調用時,設定三個必要參數:data-speed,data-type,background樣式為fixed

<div data-speed="4" data-type="background" style="background:url(...) 50% 0 no-repeat fixed">    

js會根據data-speed來計算捲動速度,越小越快。

可以根據不同的data-type作不同處理,可自訂寫代碼,很方便。

background必須是fixed。

 

parallax.js原始碼(需先引入jQuery)

 

$(document).ready(function(){                            // Cache the Window object    $window = $(window);        // Cache the Y offset and the speed of each sprite    $(‘[data-type]‘).each(function() {            $(this).data(‘offsetY‘, parseInt($(this).attr(‘data-offsetY‘)));        $(this).data(‘Xposition‘, $(this).attr(‘data-Xposition‘));        $(this).data(‘speed‘, $(this).attr(‘data-speed‘));    });        // For each element that has a data-type attribute    $(‘section[data-type="background"]‘).each(function(){                // Store some variables based on where we are        var $self = $(this),            offsetCoords = $self.offset(),            topOffset = offsetCoords.top;                // When the window is scrolled...        $(window).scroll(function() {                // If this section is in view            if ( ($window.scrollTop() + $window.height()) > (topOffset) &&                 ( (topOffset + $self.height()) > $window.scrollTop() ) ) {                    // Scroll the background at var speed                // the yPos is a negative value because we‘re scrolling it UP!                                                var yPos = -($window.scrollTop() / $self.data(‘speed‘));                                 // If this element has a Y offset then add it on                if ($self.data(‘offsetY‘)) {                    yPos += $self.data(‘offsetY‘);                }                                // Put together our final background position                var coords = ‘50% ‘+ yPos + ‘px‘;                // Move the background                $self.css({ backgroundPosition: coords });                                // Check for other sprites in this section                    $(‘[data-type="sprite"]‘, $self).each(function() {                                        // Cache the sprite                    var $sprite = $(this);                                        // Use the same calculation to work out how far to scroll the sprite                    var yPos = -($window.scrollTop() / $sprite.data(‘speed‘));                                        var coords = $sprite.data(‘Xposition‘) + ‘ ‘ + (yPos + $sprite.data(‘offsetY‘)) + ‘px‘;                                        $sprite.css({ backgroundPosition: coords });                                                                                        }); // sprites                            // Check for any Videos that need scrolling                $(‘[data-type="video"]‘, $self).each(function() {                                        // Cache the video                    var $video = $(this);                                        // There‘s some repetition going on here, so                     // feel free to tidy this section up.                     var yPos = -($window.scrollTop() / $video.data(‘speed‘));                                        var coords = (yPos + $video.data(‘offsetY‘)) + ‘px‘;                        $video.css({ top: coords });                                                                                        }); // video                            }; // in view            }); // window scroll                });    // each data-type}); // document ready

 

jQuery視差滾動外掛程式,(附原理分析,調用方法)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.