javascript - Jquery 轮播点击事件项目不会重定向

标签 javascript jquery carousel

我在这个网页中有一个轮播https://stfn.herokuapp.com它工作得几乎完美,只有主要(事件)项目位于中心,单击后不会执行任何操作(应该重定向)我尝试在 js 文件和索引中添加链接,但是没有解决问题,有人有提示吗?

[编辑]

在提问之前忘记上传最新版本,只是这样做了。所以我在index.html中的img中添加了一个a标签,但这似乎也不起作用。这是从 js 文件重定向的代码片段

    $('.carousel .item').click(function(e) {
        var index = $(this).index('li');
        carousel.cycleActiveTo(index);
        // song3();
        e.preventDefault();

        if ( currentIndex != index ){
            var difference;

            if ( currentIndex == 0 && index >= 5 ){
                difference = (index - currentIndex) - 13;
            } else {
                difference = index - currentIndex;
            }

            difference = Math.abs(difference);
            delay = difference * options.duration;
            currentIndex = index;

            console.log(delay);

            setTimeout( goToLink, delay );
        }
    });

    goToLink = function() {
        if (currentIndex == 0) {
            // console.log("works:");
            document.location.href = "about.html"
        }
        if (currentIndex == 1) {
            document.location.href = "blog.html"
        }
        else if (currentIndex == 2) {
            document.location.href = "collection.html"
        }
        else if (currentIndex == 3) {
            document.location.href = "shop.html"
        }
        else if (currentIndex == 4) {
            // alert("ABOUT2");
            document.location.href = "about.html"
        }
        else if (currentIndex == 5) {
            document.location.href = "blog.html"
        }
        else if (currentIndex == 6) {
            document.location.href = "collection.html"
        }
        else if (currentIndex == 7) {
            document.location.href = "contact.html"
        }
        else if (currentIndex == 8) {
            document.location.href = "shop.html"
        }
        else if (currentIndex == 9) {
            document.location.href = "contact.html"
        }
        else if (currentIndex == 0) {
            document.location.href = "about.html"
        }
    }

});

正如您所看到的,每个元素都有一个分配给它的索引,并且它允许切换到特定页面。事件项目的索引号为 0,但它似乎不像其他项目那样工作

最佳答案

if ( currentIndex != index ){ <-- 此检查为 false,因为两者都为零。

因此,如果检查相等,则不会执行任何操作

您需要一个 else 并调用 goto 方法。

if ( currentIndex != index ){ 
   ... the code ...
} else {
    goToLink();
}

关于javascript - Jquery 轮播点击事件项目不会重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39490043/

相关文章:

javascript - Mongoose find() 使用锁存器在 for 循环内调用

javascript - 为什么jquery中click事件调用两次

javascript - 删除脚本元素后脚本仍然运行

javascript - jQuery 选择器速度

jquery - Bootstrap 幻灯片轮播带缩略图的全屏

css - 将纯 CSS 轮播保持在固定位置

javascript - 在 jQuery 中向上遍历 DOM 以获取父元素

javascript - Observable 确定订阅者功能是否已完成

javascript - 无法在不刷新页面的情况下单击 div 中的 span #id

javascript - 试图控制同步轮播