问题
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.
回答1:
inside your .work a function, try this. Note however, that you must use the correct nivo slider html structure for .pictures
.
$('div.work').click(function(e) {
$('.pictures').show();
$('.pictures').data('nivoslider').stop().start; //Stop the Slider if it's currently running, then start it.
e.preventDefault();
});
回答2:
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.
来源:https://stackoverflow.com/questions/8388776/how-to-initiate-nivo-slider-in-ajax-loaded-content