如何在ajax加载的内容中启动Nivo Slider?
当包含 Nivo 滑块的页面已由 ajax 加载时,我无法启动 Nivo 滑块。
这是 HTML
<div class="work">
<img class="introPic" src="images/thumb.jpg" width="250" height="99" />
<h3><img class="arrow" src="images/arrow_open.gif" alt=">" /> <a class="titlelink" href="testcontent4.html" rel="address:testcontent4.html">Nivo Slider Project</a></h3>
<div class="projectIntro">
<p>This is some intro text for Nivo Slider Project</p>
</div>
<div class="pictures"></div>
</div>
这是 jQuery
$(function(){
function close_other() {
var selected_work = $('.selected_work');
selected_work.children('.pictures').empty();
$('.selected_work img.arrow').attr("src", "images/arrow_open.gif");
selected_work.removeClass("selected_work");
}
$('div.work a').click(function(e) { e.preventDefault(); });
$('div.work').bind('click', function() {
$('html,body').animate({ scrollTop: $(this).offset().top }, 500);
var href = $(this).find('a').attr('href'),
lastPart = href.split('/'),
hash = lastPart[lastPart.length - 1];
window.location.hash = hash;
var parent = $(this);
var content_holder = parent.children(".pictures");
if (parent.hasClass("selected_work")) {
close_other();
return;
}
close_other();
parent.addClass("selected_work");
content_holder.load(href + ' #ajaxContent', function() {
$(this).find('#slider').nivoSlider({
effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
animSpeed:300,
pauseTime:4000,
controlNav:true,
pauseOnHover:true //Stop animation while hovering
});
});
$('.selected_work img.arrow').attr("src", "images/arrow_close.gif");
});
});
这是现场演示: http://www.spiritlevel .co.uk/clicktest/divclicknivotest.html “Nivo Slider Project”是我想要开始工作的一个......
可能做一些基本的错误,因为我是 jQuery 的新手,所以非常感谢您能提供的任何帮助。
非常感谢。
I am having trouble getting Nivo slider to initiate when the page containing it had been loaded by ajax.
Here is the HTML
<div class="work">
<img class="introPic" src="images/thumb.jpg" width="250" height="99" />
<h3><img class="arrow" src="images/arrow_open.gif" alt=">" /> <a class="titlelink" href="testcontent4.html" rel="address:testcontent4.html">Nivo Slider Project</a></h3>
<div class="projectIntro">
<p>This is some intro text for Nivo Slider Project</p>
</div>
<div class="pictures"></div>
</div>
Here is the jQuery
$(function(){
function close_other() {
var selected_work = $('.selected_work');
selected_work.children('.pictures').empty();
$('.selected_work img.arrow').attr("src", "images/arrow_open.gif");
selected_work.removeClass("selected_work");
}
$('div.work a').click(function(e) { e.preventDefault(); });
$('div.work').bind('click', function() {
$('html,body').animate({ scrollTop: $(this).offset().top }, 500);
var href = $(this).find('a').attr('href'),
lastPart = href.split('/'),
hash = lastPart[lastPart.length - 1];
window.location.hash = hash;
var parent = $(this);
var content_holder = parent.children(".pictures");
if (parent.hasClass("selected_work")) {
close_other();
return;
}
close_other();
parent.addClass("selected_work");
content_holder.load(href + ' #ajaxContent', function() {
$(this).find('#slider').nivoSlider({
effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
animSpeed:300,
pauseTime:4000,
controlNav:true,
pauseOnHover:true //Stop animation while hovering
});
});
$('.selected_work img.arrow').attr("src", "images/arrow_close.gif");
});
});
Here is a live demo: http://www.spiritlevel.co.uk/clicktest/divclicknivotest.html
The "Nivo Slider Project" is the one I want to get working...
Probably doing something basic wrong as i am new to jQuery so would appreciate any help you can give.
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在你的 .work 函数中,试试这个。但请注意,您必须为
.pictures
使用正确的 nivo slider html 结构。inside your .work a function, try this. Note however, that you must use the correct nivo slider html structure for
.pictures
.事实证明,这里的问题不是代码,而是旧版本的 Nivo Slider 和当前版本的 jQuery (1.7.1) 之间的冲突。当我回到 jQuery 1.4.2 时,它起作用了。然后我使用当前的 jQuery 将 Nivo Slider 更新到最新版本,它也可以工作。
As it turns out, the problem here was not the code, but a conflict between an older version of Nivo Slider and the current version of jQuery (1.7.1). When I went back to jQuery 1.4.2 it worked. Then I updated Nivo Slider to the latest version with the current jQuery and it also worked.