【问题标题】:stellar.js - configuring offsets / aligning elements for a vertical scrolling website?stellar.js - 为垂直滚动网站配置偏移量/对齐元素?
【发布时间】:2012-06-18 13:57:03
【问题描述】:

我已经找到并正在尝试使用一个名为 stellar.js 的插件。主要是为网站创建视差效果,但是,我不追求这种效果——我追求的是它提供的另一种效果:

Stellar.js 最强大的功能是它对齐元素的方式。

所有元素都将返回到它们的原始位置 offset parent 与屏幕边缘相交——加或减你自己的 可选偏移量。

偏移定位示例:http://markdalgleish.com/projects/stellar.js/#show-offsets。当您滚动 div 时,它会捕捉/重新对齐到浏览器的边缘。我正试图让它适用于垂直网站。

由于我对 Javascript 和 jQuery 的新手了解,我运气不佳。我认为这只是将水平交换为垂直的情况。

有没有人玩过这个插件,或者在类似的场景中使用过它,并得到任何提示?

包含所有代码的 jsFiddle:http://jsfiddle.net/2SH2T/

以及 Javascript 代码:

var STELLARJS = {
    init: function() {
        var self = this;
        $(function(){
            self.$sections = $('div.section').each(function(index){
                $(this).data('sectionIndex', index);
            });

            self.highlightSyntax();
            self.handleEvents();

            self.debugOffsets.init();
            self.debugOffsetParents.init();

            self.initParallax();
        });
    },
    initParallax: function() {
        var isHomePage = $('body').hasClass('home'),
            $main = $('div.main');

        if (isHomePage) {
            $main.height($main.height() + $(window).height() - 1000);
        }

        if ($.browser.msie) {
            $('body').removeAttr('data-stellar-background-ratio').append('<div class="ie-bg" />');
        }

        $(window).stellar({
            horizontalOffset: !isHomePage,
            verticalScrolling: 40
        });
    },
    highlightSyntax: function() {
        $('pre').addClass('prettyprint');
        if (window.prettyPrint !== undefined) prettyPrint();
    },
    handleEvents: function() {
        var self = this,
            //Debounce function from Underscore.js
            debounce = function(func, wait) {
                var timeout;
                return function() {
                    var context = this, args = arguments;
                    var later = function() {
                        timeout = null;
                        func.apply(context, args);
                    };
                    clearTimeout(timeout);
                    timeout = setTimeout(later, wait);
                }
            },
            handleScroll = function() {
                var scrollTop = $(window).scrollTop(),
                    sectionIndex = Math.round((scrollTop - 40) / self.$sections.first().outerHeight()),
                    $activeSection = self.$sections.eq(sectionIndex);

                if ($activeSection.length === 0) {
                    $activeSection = self.$sections.last();
                }

                if ($activeSection.length === 0) return;

                $(window).unbind('scroll.stellarsite');

                if (scrollLeft === 0) {
                    $(window).unbind('scroll.stellarsite').bind('scroll.stellarsite', debounce(handleScroll, 500));
                } else {
                    $('html,body').animate({
                        scrollLeft: $activeSection.offset().left - 40
                    }, 600, 'easeInOutExpo', function() {
                        setTimeout(function(){
                            $(window).unbind('scroll.stellarsite').bind('scroll.stellarsite', debounce(handleScroll, 500));
                        }, 10);
                    });
                }

                $(window).bind('mousewheel', function(){
                    $('html,body').stop(true, true);
                });

                $(document).bind('keydown', function(e){
                    var key = e.which;

                    if (key === 37 || key === 39) {
                        $('html,body').stop(true, true);
                    }
                });
            };

        if (window.location.href.indexOf('#show-offset-parents-default') === -1) {
            $(window).bind('scroll.stellarsite', debounce(handleScroll, 500));
        }
    },
    debugOffsets: {
        init: function() {
            this.$debug = $('#debugOffsets');

            if (window.location.href.indexOf('#show-offsets') > -1) {
                this.show();
            }
        },
        show: function() {
            this.$debug.fadeIn();
            $('body').addClass('debugOffsets');
            $('h2').append('<div class="debug-h2-label">Offset Parent (All parallax elements align when this meets the offsets)</div>');
        },
        hide: function() {
            this.debug.fadeOut;
            $('body').removeClass('debugOffsets');
        }
    },
    debugOffsetParents: {
        init: function() {
            this.$debug = $('#debugOffsets');

            if (window.location.href.indexOf('#show-offset-parents-default') > -1) {
                this.removeOffsetParents();
            }

            if (window.location.href.indexOf('#show-offset-parents') > -1) {
                this.show();
            }            
        },
        show: function() {
            this.$debug.fadeIn();

            $('body').addClass('debugOffsetParents');
            $('h2').append('<div class="debug-h2-label">New Offset Parent (All parallax elements align when this meets the offsets)</div>');
            $('h2').each(function(){
                $(this).find('div.constellation:last').append('<div class="debug-constellation-label">Default Offset Parents</div>');
            });
            $('body').addClass('debug');
        },
        removeOffsetParents: function() {
            $('body').addClass('debugOffsetParentsDefault');
            $('h2[data-stellar-offset-parent]').removeAttr('data-stellar-offset-parent');
        }
    }
};

STELLARJS.init();

【问题讨论】:

标签: javascript jquery html jquery-plugins offset


【解决方案1】:

找了一阵子好的解决办法,找到了这个jQuery插件——Snappoint!

https://github.com/robspangler/jquery-snappoint

【讨论】:

    【解决方案2】:

    我修改了原始代码,并得出了与 stellarjs.com 中相同的效果。相反,它是垂直的:)

    看一看:http://jsfiddle.net/E4uVD/38/

    【讨论】:

    • 太棒了,谢谢。今晚肯定会玩这个。这些东西有什么问题吗 - 跨浏览器等?
    【解决方案3】:

    我想我已经用下面的代码实现了你所描述的。这是一个 JsFiddle:http://jsfiddle.net/E4uVD/7/

    JQuery:

    $(function(){
        var _top = $(window).scrollTop();
        var individualDivHeight = 300;
        $(window).mousedown(function() {
            $('html, body').stop();
        });
        $(window).mouseup(function(){
            var _cur_top = $(window).scrollTop();
            var totalHeight = $('#container').height();
            var posToScroll = Math.round(_cur_top / individualDivHeight) * individualDivHeight;
    
            $('html, body').stop().animate({scrollTop: posToScroll}, 2000);
        });
    });
    

    HTML:

    <div id="container">
        <div class="box">300px</div>
        <div class="box">300px</div>
        <div class="box">300px</div>
        <div class="box">300px</div>
        <div class="box">300px</div>
        <div class="box">300px</div>
        <div class="box">300px</div>
        <div class="box">300px</div>
        <div class="box">300px</div>
        <div class="box">300px</div>
    </div>
    

    CSS:

    body {
        height:2000px;
    }
    .box
    {
        color: #fff;
        height: 300px;
        width: 300px;
        border: 1px solid white;
    }
    #container {
        height:3000px;
        width:300px;
        background-color:blue;
    }
    

    【讨论】:

    • 完美,谢谢,正是我想要的!我只是将 mousedown 更改为滚动。
    猜你喜欢
    • 2013-03-07
    • 2012-06-17
    • 1970-01-01
    • 2018-05-04
    • 2019-11-23
    • 1970-01-01
    • 2014-04-02
    • 2015-07-15
    • 2013-05-29
    相关资源
    最近更新 更多