openseadragon添加click事件nextButton previousButton

时间:2017-04-26 12:12:54

标签: javascript event-handling openseadragon

我想在nextButton和previousButton中添加一个ClickListener并获取当前图像(通过viewer.source.getTileUrl()进行)。

但是,我收到了TypeError: viewer.source is null。 但它适用于getUrlBtn.addEventListener("click"[...]

我对按钮上的addHandler点击事件做了什么错误?

var viewer = OpenSeadragon({
  crossOriginPolicy: 'Anonymous',
  visibilityRatio: 1.0,
  showRotationControl: true,
  defaultZoomLevel: 1.0,
  minZoomImageRatio: 0.4,
  id: "osd-container",
  tileSources: ["images/T/1.dzi", "images/T/2.dzi", "images/T/3.dzi", "images/T/4.dzi"],
  sequenceMode: true,
  showReferenceStrip: false,
});

//works
var getUrlBtn = document.getElementById("getUrl");
getUrlBtn.addEventListener("click",function(e){
    console.log("getUrl: " + viewer.source.getTileUrl());
    currentFol.innerHTML = viewer.source.getTileUrl();
});

//TypeError: viewer.source is null
viewer.nextButton.addHandler("click",function(e){
    console.log("nextButton");
    currentFol.innerHTML = viewer.source.getTileUrl();
});
//TypeError: viewer.source is null
viewer.previousButton.addHandler("click",function(e){
    console.log("previousButton");
    currentFol.innerHTML = viewer.source.getTileUrl();
})

//I also tried but same error as above
viewer.nextButton.tracker.clickHandler = updateFol;
viewer.previousButton.tracker.clickHandler = updateFol;

var currentFol = document.getElementById("currentFol");
function updateFol(){
  console.log("next/previousButton");
  currentFol.innerHTML = viewer.source.getTileUrl();
}
<button id="getUrl">get current url</button>

<p><span id="currentFol"></span></p>

1 个答案:

答案 0 :(得分:1)

可能最好的方法是只听“页面”事件:

viewer.addHandler('page', updateFol);

这有用吗?