Nếu bạn đang tìm kiếm để bắn một sự kiện khi một quy định, hoặc mục tiêu, yếu tố cuộn vào xem, bạn có thể làm điều này bằng cách xác định các giá trị sau:
- chiều cao cửa sổ
- sự giá trị khoảng cách cuộn từ phía trên cùng của cửa sổ
- các quy định, hoặc mục tiêu, yếu tố của khoảng cách từ phía trên cùng của cửa sổ
Hãy xem xét những điều sau đây:
jQuery(window).on('scroll', function(){
var elValFromTop = Math.ceil(jQuery('.target-el').offset().top),
windowHeight = jQuery(window).height(),
windowScrollValFromTop = jQuery(this).scrollTop();
// if the sum of the window height and scroll distance from the top is greater than the target element's distance from the top, it should be in view and the event should fire, otherwise reverse any previously applied methods
if ((windowHeight + windowScrollValFromTop) > elValFromTop) {
console.log('element is in view!');
jQuery('.target-el').addClass('el-is-visible');
} else {
jQuery('.target-el').removeClass('el-is-visible');
}
});
Code Snippet diễn:
Đoạn dưới đây giải thích một ví dụ làm việc của một vấn đề chung: sửa chữa một yếu tố để các viewport khi một quy định cuộn yếu tố vào xem.
/* Sticky element on-scroll */
jQuery(window).on('scroll', function(){
var elValFromTop = Math.ceil(jQuery('.target-el').offset().top),
elHeight = jQuery('.target-el').outerHeight(),
windowHeight = jQuery(window).height(),
windowScrollValFromTop = jQuery(this).scrollTop(),
headerHeightOffset = jQuery('.fixed-header').outerHeight();
if ((windowHeight + windowScrollValFromTop) > elValFromTop) {
console.log('element is in view!');
console.log(headerHeightOffset);
jQuery('.will-change-el').addClass('fixed-el').css('top',headerHeightOffset).text('fixed');
} else {
jQuery('.will-change-el').removeClass('fixed-el').css('top','0').text('static');
}
// using template literals for multi-line output formatting
// comment out to observe when target element comes into view
console.log(`
how far is the target element from the top of the window: ${elValFromTop}
what is the target element's height: ${elHeight}
what is the window height: ${windowHeight}
what is the window's scroll value distance from the top: ${windowScrollValFromTop}
what is the sum of the window height and its offset value from the top: ${windowHeight + windowScrollValFromTop}
`);
});
/* || Reset & Defaults */
body {
margin: 0;
}
* {
box-sizing: border-box;
font-family: arial;
}
/* || General */
.target-el {
border-top: 1px solid #ccc;
}
.target-el,
.will-change-el {
transition: .7s;
text-align: center;
background: #ededed;
border-bottom: 1px solid #ccc;
}
.fixed-el {
position: fixed;
left: 0;
right: 0;
top: 0;
}
.fixed-header {
position: fixed;
left: 0;
right: 0;
top: 0;
height: 100px;
text-align: center;
background: #ededed;
border-bottom: 1px solid #ccc;
z-index: 9;
}
.buffer-el {
height: 1000px;
padding-top: 100px;
background: whitesmoke;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fixed-header">
<h1>Fixed Header</h1>
</div>
<div class="buffer-el">
<div class="will-change-el">static</div>
</div>
<div class="target-el">the target element</div>
Sandbox Ví dụ:CodePen
LƯU Ý:
này sẽ chỉ áp dụng cho di chuyển các thao tác trên tài liệu và không để bất kỳ yếu tố lồng nhau chứa trong một cửa sổ xem cuộn .
Duplicate? http://stackoverflow.com/questions/487073/jquery-check-if-element-is-visible-after-scroling – awshepard
Có thể một bản sao khác: http://stackoverflow.com/questions/1225102/jquery-event-to- trigger-action-when-a-div-được-thực hiện-nhìn thấy –