【问题标题】:jQuery script have problemsjQuery脚本有问题
【发布时间】:2018-06-16 01:52:57
【问题描述】:

我使用了我在网上找到的这个脚本 (https://codepen.io/erickarbe/pen/gyfFB) 但是在第一个循环之后它只是卡在中间并回到开始,如果你看看它不能正常工作,如果有人能帮我弄清楚它会很棒!

$jQuery.fn.liScroll = function(settings) {
  settings = jQuery.extend({
    travelocity: 0.03
  }, settings);     
  return this.each(function(){
    var $strip = jQuery(this);
    $strip.addClass("newsticker")
    var stripHeight = 1;
    $strip.find("li").each(function(i){
      stripHeight += jQuery(this, i).outerHeight(true); // thanks to Michael Haszprunar and Fabien Volpi
    });
    var $mask = $strip.wrap("<div class='mask'></div>");
    var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");                             
    var containerHeight = $strip.parent().parent().height();    //a.k.a. 'mask' width   
    $strip.height(stripHeight);         
    var totalTravel = stripHeight;
    var defTiming = totalTravel/settings.travelocity;   // thanks to Scott Waye     
    function scrollnews(spazio, tempo){
      $strip.animate({top: '-='+ spazio}, tempo, "linear", function(){$strip.css("top", containerHeight); scrollnews(totalTravel, defTiming);});
    }
    scrollnews(totalTravel, defTiming);             
    $strip.hover(function(){
      jQuery(this).stop();
    },
    function(){
      var offset = jQuery(this).offset();
      var residualSpace = offset.top + stripHeight;
      var residualTime = residualSpace/settings.travelocity;
      scrollnews(residualSpace, residualTime);
    });         
  });   
};

$(function(){
  $("ul#ticker01").liScroll();
});

【问题讨论】:

  • it just gets stuck in the middle and goes back to the start, - 是什么? if you give it a look it doesnt work properly - 为什么?它应该做什么?它做了什么?代码完全按照它编写的方式执行 - 所以说“不能正常工作”并不意味着代码是错误的,这意味着您将它用于错误的目的......“我在网上找到的脚本” - 什么当您找到该脚本时,您是否正在寻找?是什么让你认为别人的脚本做了你想做的事?您需要提供详细信息
  • 好吧,我希望文本以一种平滑的方式一遍又一遍地滚动,它的作用是它只滚动一次,然后它到达列表的中间,然后跳到开头

标签: javascript jquery html scroll news-ticker


【解决方案1】:

我发现以下迭代与第一次不同的唯一方法是它们停止在列表的末尾,而不是再次滑到屏幕上。

这是因为当 scrollnews 递归调用自身以重新启动时,它假定它只需要按字面意思移动它被告知的高度,而忽略在以额外的正偏移量重新开始时拾取的额外滚动距离。因此,您需要将 scrollnews 调用更改为 scrollnews(totalTravel + containerHeight, defTiming)

不过,这可能会影响他们设置的时间比率。

【讨论】:

  • 当我在第一个循环之后添加 containerHeight 时,它就停止了继续并保持空白......
  • 所以你有这条线? $strip.animate({top: '-='+ spazio}, tempo, "linear", function(){$strip.css("top", containerHeight); scrollnews(totalTravel + containerHeight, defTiming);});
猜你喜欢
  • 2012-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
相关资源
最近更新 更多