ala*_*agu 13 html javascript css jquery
我有如下的Html代码,
<div data-stored="storenow" data-save="save" class="saveIcon" data-unique="game">Save</div>
Run Code Online (Sandbox Code Playgroud)
我写了jquery滚动到gameNo 456,如下所示.
var container = $("html,body");
var scrollTo = $(this).find('.saveIcon').attr('data-unique', 456);
container.animate({
scrollTop: scrollTo.offset().top - container.offset().top + container.scrollTop()
});
Run Code Online (Sandbox Code Playgroud)
我使用的是jQuery 1.9版.我在控制台中收到错误:
无法读取未定义的属性"top"
是不是可以滚动到类名而不是id?
但它在Firefox中运行良好.但不是在Chrome或IE中.
我尝试从stackoverflow中找到解决方案.但所有其他解决方案都不同于我的情况.
小智 27
您没有定位DOM对象,而是以字符串为目标.
scrollTo = $(this).find('.saveIcon').attr('data-unique', 456); -> this is wrong
Run Code Online (Sandbox Code Playgroud)
因此,在尝试定位元素时,实际上是将'data-unique'设置为'.saveIcon'元素.
试试这个:
scrollTo = $('.saveIcon');
Run Code Online (Sandbox Code Playgroud)
工作代码:
var $container = $("html,body");
var $scrollTo = $('.saveIcon');
$container.animate({scrollTop: $scrollTo.offset().top - $container.offset().top + $container.scrollTop(), scrollLeft: 0},300);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37989 次 |
| 最近记录: |