我有以下结构的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>
当我单击图像本身(当前显示的幻灯片的图像)时,放大/缩小工作。但是,当我单击+
图像上的按钮 span 时,我在 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