我想在 nextButton 和 previousButton 中添加一个 click EventListener 并获取当前图像(通过 进行viewer.source.getTileUrl()
)。
但是,我收到TypeError: viewer.source is null
. 但它适用于getUrlBtn.addEventListener("click"[...]
我对按钮上的 addHandler click 事件做错了什么?
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>