javascript - jQuery脚本有问题
问题描述
我使用了我在网上找到的这个脚本(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();
});
解决方案
我发现以下迭代与第一次不同的唯一方法是它们停止在列表的末尾,而不是再次滑到屏幕上。
这是因为当 scrollnews 递归调用自身重新启动时,它假定它只需要按字面意思移动它被告知的高度,忽略在以额外的正偏移量重新开始时拾取的额外滚动距离。因此,您希望将 scrollnews 调用更改为scrollnews(totalTravel + containerHeight, defTiming)
不过,这可能会影响他们设定的时间比率。
推荐阅读
- python - 将 pandas json 列转换为多行
- excel - 附加剪切和删除循环 VBA
- typescript - 打字稿中的 Enum.GetValues(typeof(TEnum))
- hibernate - 当条件暗示日期时,休眠查询缓存不起作用
- database-replication - Postgres11 和 Postgres12 之间的副本
- javascript - 如果存在值,则使用 javascript 检查 json 数据
- python - 如何有效地将多json数组转换为python中的列表避免for循环
- sql - Oracle Distinct 查询 - 获取年度不同值
- javascript - 从 nuxt.js 中引用的子元素列表中访问单个元素
- javascript - 使用 shadow dom 和 chartjs 创建 Widget 的问题