Javascript滚动以仅将事件应用于父div
问题:我有两个具有相同内容的div,其中有多个锚(可滚动),如果在一个div中单击滚动触发按钮,两个div内容都在滚动,但只有一个(单击的地方)应该滚动。 如何将事件绑定到单击触发器的div?尝试使用$(此)但无法找到它 代码如下:Javascript滚动以仅将事件应用于父div,javascript,jquery,html,event-handling,scrollto,Javascript,Jquery,Html,Event Handling,Scrollto,问题:我有两个具有相同内容的div,其中有多个锚(可滚动),如果在一个div中单击滚动触发按钮,两个div内容都在滚动,但只有一个(单击的地方)应该滚动。 如何将事件绑定到单击触发器的div?尝试使用$(此)但无法找到它 代码如下: <div class="tile_list_wrapper"> <div class="tile__list"> <a class="quickmark" href="https://google.com" target="Goo
<div class="tile_list_wrapper">
<div class="tile__list">
<a class="quickmark" href="https://google.com" target="Google" ondrop="checkondrop();"><img src="../../images/add_icon.png"></a>
<a class="quickmark" href="https://google.com" target="Google" ondrop="checkondrop();"><img src="../../images/add_icon.png"></a>
<button class="tile__more">more </button>
</div>
</div>
<div class="tile_list_wrapper">
<div class="tile__list">
<a class="quickmark" href="https://google.com" target="Google" ondrop="checkondrop();"><img src="../../images/add_icon.png"></a>
<a class="quickmark" href="https://google.com" target="Google" ondrop="checkondrop();"><img src="../../images/add_icon.png"></a>
<button class="tile__more">more </button>
</div>
</div>
<script>
var $quickmarks = $('.tile__list').children('a');
var $chScrollPositions = new Array();
$quickmarks.each(function(i){
$chScrollPositions[i] = Math.round($(this).offset().top - $('.tile__list').offset().top) - 8;
});
$quickmarks.eq(0).addClass('active');
$('button').click(function(){
var last = $quickmarks.parent().find('a.active').removeClass('active').index();
var next;
switch($(this).index()){
case 1:
next = (last + 4 == $quickmarks.length) ? 0 : last + 4; // Loop around to first
}
$quickmarks.eq(next).addClass('active');
$('.tile_list_wrapper').scrollTo($chScrollPositions[next]);
});
</script>
更多
更多
var$quickmarks=$('.tile\uu list')。children('a');
var$chScrollPositions=新数组();
$quickmarks。每个功能(i){
$chScrollPositions[i]=数学圆($(this).offset().top-$('.tile\u list').offset().top)-8;
});
$quickmarks.eq(0).addClass('active');
$(“按钮”)。单击(函数(){
var last=$quickmarks.parent().find('a.active').removeClass('active').index();
var-next;
开关($(this).index()){
案例1:
next=(last+4==$quickmarks.length)?0:last+4;//循环到第一个
}
$quickmarks.eq(next.addClass('active');
$('.tile\u list\u wrapper')。滚动到($chScrollPositions[next]);
});
行$('.tile\u list\u wrapper')。滚动到($chScrollPositions[next])代码>将选择具有平铺\u列表\u包装类的所有项目
将其更改为:
var $thisTile = $(this).siblings('.tile_list_wrapper');
$($thisTile).scrollTo($chScrollPositions[next]);
你能做一把小提琴吗?只需要摆好一把小提琴…看一看,每个按钮都会触发两个框来滚动。谢谢!它正在使用该调整;)现在我只注意到一件事……它仍然将第二个div中的其他锚设置为“活动”。这里是你可以看到的小提琴-对JS来说还是新的,这就是为什么我请求一些帮助。官方文档太难理解,初学者无法停留在当前的tile\u列表中\u包装更改以下行:next=(last+4==$quickmarks.length)?0:last+4;to:next=(last+4==$(this).同级('.tile\u list\u wrapper')。查找('a')。长度)?0:last+4;谢谢你的评论,但这还不行:-/我的想法是这个var$quickmarks=$('.tile_u列表')。children('a')代码>必须定义得更具体,但不能确定。这不是varvar last=$quickmarks.parent().find('a.active').removeClass('active').index()吗代码>是否也应用于所有元素?伙计们JS让我困惑x-Dedite你是对的它需要分解成单独的元素SexCellent!非常感谢你的帮助!最后一把小提琴很好用!