jquery - 图像 slider jQuery 中的动画错误

标签 jquery

我正在尝试在 jquery 中制作图像 slider 。我写了下面的代码。我无法理解如何为图像设置动画而不是更改 src 属性。

https://jsfiddle.net/2tsfnauk/2/

var tyInterval = setInterval(function() {

    $('#first').attr('src', $('#images li').eq(0).children().attr('src'))
    $('#head').html($('#images li').eq(0).children().attr('alt'))

    var iHtml = $('#images li').eq(0).remove();
    $('#images').append(iHtml);


}, 2000)

在页面的初始加载中,会显示一个空的 div。这是什么原因

最佳答案

如果您尝试以下代码会怎样?

$(document).ready(function() {
  var carousel= $('#carousel');
  var head= $('#head');

  // variable will be used to loop on all images
  var count = 1;
  // find all images inside the ul with id = images
  var max = $('#images img').length;
  // make sure that the ul is hidden
  $('#images').hide();
  var tyInterval = setInterval(function() {
    var html = $($('#images li').eq(count).html());
    carousel.append(html);
    head.text(html.attr('alt'))
    count = count +1;
    if(count==max) // when you reach the last image then reset the counter
        count=0;
  }, 2000)
  // to load for the first time
    var html = $('#images li').eq(0).html();
    carousel.append(html);
    head.text(html.attr('alt')
})

这里是您的 demo 的更新信息

希望这对你有帮助

关于jquery - 图像 slider jQuery 中的动画错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39432400/

相关文章:

javascript - 如何获取元素中的文本以及其中的图像 (JQuery)?

javascript - 在元素克隆上隐藏/显示不适用于克隆元素

javascript - 为什么我的 jQuery getJSON 调用(在 Office.js(Excel js 加载项)内)返回错误?

javascript - 如何在 html 中打开 MS Office Outlook

javascript - 下拉检查列表单选取消选择

javascript - ngModel - 如何处理它在不同浏览器中的不同行为?

javascript - 为什么实际网站会在重定向到另一个网站之前显示?

javascript - 将边框应用于 SlidesJS 框架不起作用

javascript - 根据属性值 angularJs 将类添加到特定列表项

javascript - 仅在 X 页面上运行函数