【问题标题】:With Uranium.io's zoom interaction, what is required for the zoom in/out buttons to find the image to zoom in/out from?使用 Uranium.io 的缩放交互,放大/缩小按钮需要什么才能找到要放大/缩小的图像?
【发布时间】:2014-07-24 03:34:27
【问题描述】:

我有以下结构的 DOM 元素:

<div class="flexslider" data-ur-set="zoom">
    <div data-ur-zoom-component="loading" data-ur-state="disabled">Loading…</div>
    <div data-ur-zoom-component="button" data-ur-state="disabled">
        <span>+</span><span>−</span>
    </div>
    <ul class="slides" data-ur-zoom-component="view_container">
        <li><img src="//mydomain.com/path/to/small/image" onerror="this.src=altView.src" class="thumbnail selected-thumbnail" data-ur-src="//mydomain.com/path/to/large/image" data-ur-zoom-component="img"></li>
        <li><img src="//mydomain.com/path/to/small/image" onerror="this.src=altView.src" class="thumbnail" data-ur-src="//mydomain.com/path/to/large/image" data-ur-zoom-component="img"></li>
        <li><img src="//mydomain.com/path/to/small/image" onerror="this.src=altView.src" class="thumbnail" data-ur-src="//mydomain.com/path/to/large/image" data-ur-zoom-component="img"></li>
    </ul>
</div>

当我单击图像本身(当前显示的幻灯片的图像)时,放大/缩小工作。但是,当我单击图像上的 + 按钮跨度时,我在 Javascript 控制台中看到一个错误,显示为 TypeError: $img.data(...) is null

function setActive(img) {
    if ($img && img != $img[0]) {
        self.state = "enabled-out";
        var zoomImg = $img.data("urZoomImg");
        zoomImg.transform(0, 0, 1);
        zoomImg.transitionEnd();
    }
    $img = $(img);
}

// zoom in/out button, zooms in to the center of the image
$(self.button).on(touchscreen ? "touchstart.ur.zoom" : "click.ur.zoom", function() {
    if (self.img.length > 1)
        setActive($(self.img).filter($container.find("[data-ur-state='active'] *"))[0]);
    else
        setActive(self.img[0]);
    $img.data("urZoomImg").zoom(); // <---- This is the line throwing the error
});

我已经在上面的代码中指出了null TypeError 的抛出位置。我跟踪它,发现 setActive 正在发送一个undefined img 参数。

我怀疑我没有正确获取 Uranium Zoom 交互的数据属性,但为什么 + 按钮会抛出此错误?

正确的行为类似于此处的示例:http://uranium.io/interactions/zoom.html

【问题讨论】:

  • 你好萨加尔。目前我正在逐步使用 Firebug 中的断点和观察表达式。我正在查看 $container.find("[data-ur-state='active'] *") 似乎找不到任何图像,因此过滤器返回一个空列表。

标签: javascript moovweb


【解决方案1】:

对于那些发现自己处于我的位置的人,我发现了正在发生的事情,这为我提供了如何为自己解决问题的线索。

如果滑块中有多个图像,Uranium zoom 希望它们排列在 Uranium carousel 中。 carousel DOM 结构意味着包含每个图像的元素将具有data-ur-state='active',因此选择器$container.find("[data-ur-state='active'] *") 将在当前显示的幻灯片元素中找到图像。

但是,如果您不使用轮播,那么您可能会看到我看到的null TypeError。要在不使用 Uranium carousel 的情况下解决此问题,有一种方法可以更改核心 Uranium 文件以捕获适合您项目的选择器。但是,您正在更改核心文件,因此请小心处理!

要更改的文件名为assets/javascript/.remote/http__colon____slash____slash__downloads.moovweb.com__slash__uranium__slash__1.0.167__slash__uranium-pretty.js

修改第 608 行的选择器(如果您有类似的 Uranium 版本,代码块如下所示)。

605     // zoom in/out button, zooms in to the center of the image
606     $(self.button).on(touchscreen ? "touchstart.ur.zoom" : "click.ur.zoom", function() {
607       if (self.img.length > 1)
608         setActive($(self.img).filter($container.find("[data-ur-state='active'] *"))[0]);
609       else
610         setActive(self.img[0]);
611       $img.data("urZoomImg").zoom();
612     });

重新启动您的 moov 服务器,使其生成新的assets/javascript/main.js,您的缩放按钮将使用新代码并正常工作!

【讨论】:

    猜你喜欢
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 2023-01-09
    • 2015-11-28
    • 2017-12-05
    • 1970-01-01
    相关资源
    最近更新 更多