[英]Scroll to the particular div by Clicking on the div on any Point
I know this question has been answered but the scenario of my question is quite different, so I will try to explain it. 我知道这个问题已经得到回答,但是我的问题的情况大不相同,因此我将尽力对其进行解释。 If anyone wants more explanation I would love to give it.
如果有人想要更多的解释,我很乐意提供。
I have a filter bar that consists of 8 boxes. 我有一个由8个框组成的过滤器栏。
<div class="filter-box filter-box--green px-2 pt-3 pb-4" data-box-id="web-dev">
<img width="60" height="60" src="{!! asset('images/Service-page/Web Development-hover.svg') !!}" alt="Web Dev" />
<div class="title text-center mt-2 pb-4">
Web<br />Development
</div>
<div class="arrow">
<img src="{!! asset('images/Service-page/Green.svg') !!}">
</div>
</div>
When I am clicking on the box, it scrolls to the specific div based on the data-box-id
当我单击该框时,它会根据
data-box-id
滚动到特定的div
Below is the JavaScript code that scrolls to the particular div. 以下是滚动到特定div的JavaScript代码。
<script type="text/javascript">
$(document).ready(function() {
$('.filter-box').on('click', function(e) {
// console.log(window.scroll)
var boxId = $(this).attr('data-box-id');
$('html, body').animate({
scrollTop: $("#" + boxId).offset().top
}, 'slow');
});
})
</script>
Here I used offset
. 在这里,我使用
offset
。 The problem is that sometimes, the cursor is not at target. 问题在于,有时光标没有对准目标。 What I want to achieve is that when I click anywhere on the filter box, it should scroll and my cursor position should be on the expected div.
我想要实现的是,当我单击过滤器框上的任意位置时,它应该滚动并且光标位置应该在预期的div上。
Use Jquery .animate to have a smooth scroll to the desired element see the example. 使用Jquery .animate可以平滑滚动到所需元素,请参见示例。
$( "#notelement" ).click(function() { $('html, body').animate({ scrollTop: ($('#element').offset().top) },500); });
#notelement{ margin-bottom:800px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="notelement">Other element</div> <div id="element">Scroll to element</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.