【问题标题】:Using jQuery "swipe" function to navigate an array of images使用 jQuery “swipe” 功能来导航图像数组
【发布时间】:2015-05-13 19:02:33
【问题描述】:

我正在构建一个简单的幻灯片,当在计算机上查看时由按钮控制,在触摸屏设备上通过滑动手势控制。 This is a demo with 3 images

每张图片、其对应的标题和导航都包含在一个 div 中。这是第一个:

<div class="item" id="1">
    <img src="...">
    <div class="caption">
        caption 1
    </div>
    <div class="navigation">
        <a href="#" id="1prev">&lt</a> 1 / 3 <a href="#" id="1next">&gt</a>
    </div>
</div>

这些 div 使用“单击”和“向左滑动/向右滑动”功能显示或隐藏。

$(document).ready(function () {
    $("#1prev").click(function () {
        $("#1").hide();
        $("#3").show();
    });
    $("#1").on("swipeleft", function () {
        $("#1").hide();
        $("#3").show();
    });
    $("#1next").click(function () {
        $("#1").hide();
        $("#2").show();
    });
    $("#1").on("swiperight", function () {
        $("#1").hide();
        $("#2").show();
    });
});

幻灯片总共将包含多达 40 张图片。有没有办法压缩脚本?这是一个相对有效且易于访问的解决方案吗?代码是否正确编写?可以改进吗?

【问题讨论】:

  • 谢谢。如何将按钮的“单击”功能添加到该代码中?当我到达数组的末尾时,如何让它循环回到开头?我可以调用什么函数来显示下一个图像或上一个图像而不淡入/淡出?

标签: android jquery ios jquery-mobile swipe


【解决方案1】:

你可以这样做:

对于这些项目,我已经为 prev 和 next 按钮分配了类而不是 ID。

<div class="item" id="1">
    <img src="http://www.leecorbin.co/img1.jpg" width="50%" />
    <div class="caption">caption 1</div>
    <div class="navigation"> 
        <a href="#" class="prevBtn">&lt</a> 
        1 / 3 
        <a href="#" class="nextBtn">&gt</a>
    </div>
</div>

然后在脚本中,pagecreate

隐藏所有项目并仅显示第一个项目。 在项目上为向左滑动和向右滑动添加处理程序。 为导航按钮添加点击处理程序 在这些处理程序中,确定我们前进的方向以及我们当前在哪张幻灯片上。 调用传入方向和当前幻灯片的函数;它确定要显示的下一张幻灯片并进行过渡。

$(document).on("pagecreate", "#page1", function () {
    $(".item").hide().first(0).show();

    $(document).on("swipeleft swiperight", ".item", function (e) {
        var dir = 'prev';
        if (e.type == 'swipeleft') {
            dir = 'next';
        }
        GoToNextSlide($(this), dir);
    });

    $(document).on("click", ".navigation > a", function (e) {
        var dir = 'prev';
        if ($(this).hasClass("nextBtn")) {
            dir = 'next';
        }
        var $item = $(this).closest(".item");
        GoToNextSlide($item, dir);
    });

});

function GoToNextSlide($item, direction) {
    var $next;
    if (direction == 'next') {
        if ($item.next().length > 0) {
            $next = $item.next();
        } else {
            $next = $(".item").first();
        }
    } else {
        if ($item.prev().length > 0) {
            $next = $item.prev();
        } else {
            $next = $(".item").last();
        }
    }
    $item.fadeOut(function () {
        $next.fadeIn();
    });
}

更新DEMO

【讨论】:

  • 我认为您的代码运行良好。现在我必须让它与this function 合作,以响应地在页面上居中图像。 Here is your demo with a few adjustments.我怎样才能使它与附加脚本一起工作?
  • 添加到我的最后一条评论 - I added the script that centers the image to my first demo 并发现虽然它仍然无法正常工作(只有数组中的第一个图像居中,其他图像显示在页面底部)它比当它添加到您的解决方案中时。我希望这里有线索。
  • @DBenway,给你:jsfiddle.net/ezanker/ej40c8m7/11 在 jQM 中使用 pagecreate 之类的事件,而不是 document.ready 和窗口加载。此外,由于在调用 doresize 时其他图像被隐藏,因此除了第一项之外,其他所有图像的计算都失败。而是使用这里的 CSS:zerosixthree.se/…
  • 真诚感谢@ezanker。 The updated fiddle 添加 -webkit-transform: translateY(-50%); 后在 iPhone 5 和 FF、Chrome 和 Safari 上运行良好到 div.item > img 它在 IE10 中存在错误,我怀疑这是 CSS 的问题。我觉得不受欢迎的是,当触摸屏笔记本电脑上的滑动手势导航阵列时,它还会选择页面上的图像和所有文本。我该如何阻止它这样做?
  • 我还希望通过简单地触摸(除了滑动)屏幕的左侧或右侧部分来导航阵列。我怎样才能做到这一点?
猜你喜欢
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多