如何解决ScrollTop 在砌体图像缩略图中找不到几个不同的数字元素 ID 之一来滚动页面到图像
我有一组砖石布局的缩略图,每个缩略图都链接到一个画廊页面。我想返回显示单击图像的滚动状态。我正在使用图库上的关闭按钮向地址添加一个哈希值,然后我用它滚动到图像。代码始终只选择要滚动的 div 的第一个实例。如果我包含一个可点击的链接,页面就会正确滚动。
html
<div class="item">
<div id="photo_335" class="anchor"></div>
<a href="gallery.php?gallNo=335&catNo=14"><img src="images/gall335.jpg" </a>
</div>
<div class="item">
<div id="photo_332" class="anchor"></div>
<a href="gallery.php?gallNo=332&catNo=14"><img src="images/gall332.jpg"></a>
</div>
<div class="item">
<div id="photo_323" class="anchor"></div>
<a href="gallery.php?gallNo=323&catNo=14"><img src="images/gall323.jpg"></a>
</div>
脚本
var $hash = window.location.hash;
var $photo = "photo_" +$hash.replace('#','');
if ($hash.length) {
$('html,body').stop().animate({
scrollTop: $($target).offset().top - headerHeight
},10);
return false;
}
site.com/test.php?catNo=14#337
解决方法
好的解决了我的问题。我在布局完成之前触发了脚本。 console.log( $('#'+$photo).offset() );
给了我 div 的位置并帮助我意识到这一点。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。