如何在ajax加载的内容中启动Nivo Slider?

发布于 2024-12-19 12:39:40 字数 2075 浏览 2 评论 0原文

当包含 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="&gt;" /> <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月蹉跎了容颜 2024-12-26 12:39:40

在你的 .work 函数中,试试这个。但请注意,您必须为 .pictures 使用正确的 nivo slider html 结构。

$('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(); 
});

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(); 
});
骄兵必败 2024-12-26 12:39:40

事实证明,这里的问题不是代码,而是旧版本的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文