【问题标题】:Custom Javascript Thumbnail Slider自定义 Javascript 缩略图滑块
【发布时间】:2019-03-20 12:09:29
【问题描述】:

我一直致力于使用 data-src 创建自定义 javascript 缩略图滑块。一切正常,只是下一个按钮和上一个按钮根本不起作用。任何帮助将不胜感激。

这里是codepen的链接

https://codepen.io/mandipluitel/pen/BbOXgZ

我正在使用此代码。

$(document).ready(function(e){
  $(".thumbnails li").click(function(e){
    var thisDataSrc = $(this).attr("data-src");
    var thisDataCount = $(this).children().attr("data-count");
    if(thisDataSrc == "undefined"){}
    else{
      $(this).siblings("li").removeClass("active");
      $(this).addClass("active");
      $(this).parents(".thumbnails").next().find("img").attr("src",thisDataSrc);
      $(this).parents(".thumbnails").next().find("span.number").html("" + thisDataCount + "");
    }
  });

  $("button.next").click(function(e) {
    var currentDiv = 0;

    var newDataSrc = $(this).parents(".slider-hold").prev().find(".active").attr("data-src");
    if(newDataSrc == "undefined"){}
    else{
      currentDiv = currentDiv .next();
      currentDiv.siblings("li").removeClass("active");
      currentDiv.addClass("active");
      currentDiv.parents(".thumbnails").next().find("img").attr("src",newDataSrc);
    }
  });
});

在这里提供帮助。

【问题讨论】:

  • 在浏览器中测试控制台是否有错误?
  • 请同时包含您在问题中收到的任何错误消息,而不是仅仅说“它不起作用”。这将帮助我们帮助您更快地得到答案!
  • @AndyG 不,没有任何控制台错误。你能检查一下codepen url吗?我更新了变量,下一个按钮正在工作,但它不同步,而且它只有在我第一次点击两次时才起作用。
  • @remy_rm 很抱歉这里的混乱。但实际上没有错误消息,您能检查一下codepen url吗?

标签: javascript jquery


【解决方案1】:

问题是 currentDiv 设置为零。因此,当您稍后将其设置为 currentDiv.next() 时,它实际上并没有做任何事情。

我相信函数应该像这样开始:

$("button.next").click(function(e) {
    var currentLi = $("li.active");
    var nextLi = currentLi.next();
    if (nextLi) {
        nextLi.siblings("li").removeClass("active");
        nextLi.addClass("active");
        $("#large-image").attr("src",nextLi.attr("data-src"));
    }
});

【讨论】:

  • 啊,很抱歉,谢谢,我忘了指定正确的变量,它已添加,但我仍然遇到同样的问题。下一个按钮只有在我第一次单击两次时才起作用,并且活动缩略图和大图像的协调完全错误。
  • 啊,我以为我只会把你推向正确的方向:-) 我已经更新了答案。我建议在 html 元素中添加“id”属性,这样您就可以使用 jquery 选择器,如“#large-image”,而不是搜索它们。
猜你喜欢
  • 1970-01-01
  • 2012-09-13
  • 1970-01-01
  • 2022-09-29
  • 2012-08-30
  • 2012-06-14
  • 2014-04-27
  • 1970-01-01
  • 2013-10-19
相关资源
最近更新 更多