我正在使用 Fotorama 4.5.0 (Rails gem),但在获取 API 对象时遇到问题。
在页面加载时 Fotorama 位于隐藏的 div 中。它在点击时以模式打开。
这是代码(咖啡):
$objects = $('*[rel="fotorama"]')
$fotorama_div = $('.fotorama')
$fotorama_div
.on('fotorama:showend', (e, fotorama, extra) ->
// resizing fotorama
).fotorama()
fotorama = $fotorama_div.data('fotorama')
console.log(fotorama) // here is undefined
$objects.on 'click', ->
// finding index of an image
modal.open({content: $('#fotorama-container')})
fotorama.show(index) // index is ok
有了这个代码fotorama
对象总是undefined
.
好的,我添加if
了检查:
$objects = $('*[rel="fotorama"]')
$fotorama_div = $('.fotorama')
$fotorama_div
.on('fotorama:showend', (e, fotorama, extra) ->
// resizing fotorama
).fotorama()
fotorama = $fotorama_div.data('fotorama')
console.log(fotorama) // here is undefined
$objects.on 'click', ->
// finding index of an image
modal.open({content: $('#fotorama-container')})
if typeof fotorama == "undefined"
fotorama = $fotorama_div.data('fotorama')
console.log(fotorama) // here is undefined after the first click on object
fotorama.show(index) // index is ok
在第一次单击对象之后 -fotorama
仍然是undefined
,但是在我关闭模式并进行第二次单击之后 - 我得到fotorama
对象并且一切都按预期工作。
当我$fotorama_div.data('fotorama')
在控制台中进行操作时,它可以工作。
如何fotorama
在页面加载时获取对象?