【问题标题】:Creating a jQuery vertical scroll loop创建一个 jQuery 垂直滚动循环
【发布时间】:2015-07-05 13:03:42
【问题描述】:

我正在尝试编写一个 jQuery 脚本,该脚本将在它们的 <div> 父级中不断滚动这些 <p> 标记。我是从this site 那里得到这个想法的。

function shuffle(){

    $('#wrapper > p').each(function(){
        h = ($(this).offset().top + 130);
        if( h > 500 ){
            console.log(h);
            $(this).css ('top', 0 );
            return;
        }

        if( h == 270 ){
            $(this).addClass('current' );
        }

        if( h == 360 ){
            $(this).removeClass('current' );
        }
        
        $(this).animate({
            top: h,
            easing: 'easeIn'
        }, 500, '');
        
        if( h > 1350 ){
            $(this).css ('top', 0 );
        }
    });

    window.setTimeout(shuffle, 2500);
}

var i = 0;
        
$('#wrapper > p').each(function(){
    starter = $(this).css('top', ((i * 90) ) + 'px' );
    starterWhite = ($(this).offset().top + 130);
    if((i*90) == 270)
        $(this).addClass('current');
    $(this).css('top', starter );
    i++;
});

window.setTimeout(shuffle, 2000);
#wrapper {
    height: 500px;
    overflow: hidden;
    position: relative;
}

p {
    position: absolute;
    margin: 0;
    padding: 0;
}

.current {
    color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
    <p>1</p>
    <p>2</p>
    <p>3</p>
    <p>4</p>
    <p>5</p>
    <p>6</p>
    <p>7</p>
</div>

问题是当我尝试让它滚动&lt;p&gt; 标签重叠时,突出显示的标签不会改变。知道如何使它工作吗? Here's a JSFiddle 到目前为止我得到了什么。

【问题讨论】:

  • 另外,我创建的 JSFiddle 主要基于他的 .js file here 中的代码。
  • 你看到我的回答了吗?

标签: javascript jquery html css scroll


【解决方案1】:

签出this

// Constants, you can play with it
var STEP = 90;
var CURRENT_INDEX = 3;

// Variables for calculating
var currentTop = STEP * CURRENT_INDEX;
var nextForCurrentTop = STEP * (CURRENT_INDEX + 1);
var $numbers = $('#wrapper > p');
// In this case = 7
var count = $numbers.length;
var secondToLastTop = STEP * (count - 1);

$numbers.each(function(i) {
    var $this = $(this);
    $this.css('top', i * STEP + 'px');

    if (i === CURRENT_INDEX) {
        $this.addClass('current');
    }
});

window.setTimeout(shuffle, 2000);

function shuffle() {
    $numbers.each(function() {
        $this = $(this);
        // Calculating the new position of the element
        h = parseInt($this.css('top')) + STEP;

        // In this case = 540
        if (h > secondToLastTop) {
            $this.css('top', 0);
            return;
        }

        // In this case visually = 3rd element
        if (h === currentTop) {
            $this.addClass('current');
        }

        // In this case visually = 4th element
        if (h === nextForCurrentTop) {
            $this.removeClass('current');
        }

        $this.animate({
            top: h,
            easing: 'easeIn'
        }, 500, '');
    });

    window.setTimeout(shuffle, 2500);
}
#wrapper {
    height: 500px;
    overflow: hidden;
    position: relative;
}

p {
    position: absolute;
    margin: 0;
    padding: 0;
}

.current {
    color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
    <p>1</p>
    <p>2</p>
    <p>3</p>
    <p>4</p>
    <p>5</p>
    <p>6</p>
    <p>7</p>
</div>

我改进并注释了您示例中的代码,删除了幻数。您可以使用常量。

【讨论】:

  • 是的,这就是问题所在,但现在他知道这些数字是硬编码的,因此他可以根据自己的需要进行修改:)
  • 谢谢!我正在尝试将此代码合并到我的实际网站中,但我仍然遇到堆叠问题。您是如何找出需要哪些值的?例如 javascript 中的第 3 行或第 5 行。
  • @Sosa 我改进/简化/注释了我的代码,现在看看。
  • 非常感谢它完美地工作!非常感谢您花时间编辑代码并编写这些 cmets。它也适用于我的其他网站,因此不再有这种堆叠。真男人,干得好。
  • @Sosa 很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-20
  • 1970-01-01
  • 1970-01-01
  • 2011-12-06
  • 2018-02-08
  • 2022-12-24
  • 1970-01-01
相关资源
最近更新 更多