gpt4 book ai didi

javascript - 如何修复滚动上的 jQuery 图像序列

转载 作者:行者123 更新时间:2023-11-30 14:22:55 26 4
gpt4 key购买 nike

我尝试实现一个 javascript 函数,在滚动时为图像序列设置动画。

我尝试使用此链接中的脚本创建动画:https://www.jqueryscript.net/animation/jQuery-Plugin-To-Create-Image-Sequence-Animation-On-Scroll-Sequencer.html

我的问题是这个脚本已经三年了,不能与 jQuery 3.2.1 一起使用。但我必须使用这个更新得多的 jQuery 版本。

脚本代码如下所示:

/**
* jQuery-Sequencer
* https://github.com/skruf/jQuery-sequencer
*
* Created by Thomas Låver
* http://www.laaver.com
*
* Version: 2.0.0
* Requires: jQuery 1.6+
*
*/

(function($) {

$.fn.sequencer = function(options, cb) {

var self = this,
paths = [],
load = 0,
sectionHeight,
windowHeight,
currentScroll,
percentageScroll,
index;

if(options.path.substr(-1) === "/") {
options.path = options.path.substr(0, options.path.length - 1)
}

for (var i = 0; i <= options.count; i++) {
paths.push(options.path + "/" + i + "." + options.ext);
}

$("<div class='jquery-sequencer-preload'></div>").appendTo("body").css("display", "none");

$(paths).each(function() {
$("<img>").attr("src", this).load(function() {
$(this).appendTo("div.jquery-sequencer-preload");
load++;
if (load === paths.length) {
cb();
}
});
});

$(window).scroll(function() {
sectionHeight = $(self).height();
windowHeight = $(this).height();
currentScroll = $(this).scrollTop();
percentageScroll = 100 * currentScroll / (sectionHeight - windowHeight);
index = Math.round(percentageScroll / 100 * options.count);
if(index < options.count) {
$("img.sequencer").attr("src", paths[index]);
}
});

return this;

};

}(jQuery));

所以我已经更改了第 37 行

$("<img>").attr("src", this).load(function() {

$("<img>").attr("src", this).on("load", function() {

通过此更改,我的所有图像都已加载,但我仍然收到错误消息:Uncaught TypeError: cb is not a function

我还必须更改什么,才能使脚本再次运行?

感谢任何提示。

最佳答案

cb() 是一个回调函数,一旦预加载器完成获取图像就会被调用。

根据您链接到的示例,回调是:

  function() {
$("div#preload").hide();
}

这很简单地隐藏了预加载器消息。

在上下文中,插件被初始化为:

  $("div#images").sequencer({
count: 128,
path: "./images",
ext: "jpg"
}, function() {
$("div#preload").hide();
});

由于您没有提供调用此函数的方式,我怀疑您缺少此函数回调。

关于javascript - 如何修复滚动上的 jQuery 图像序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52444360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com