Wenn Sie schauen, um ein Ereignis zu feuern, wenn ein bestimmtes oder Zielelement scrollt in Sicht, können Sie dies tun, indem Sie die folgenden Werte zu bestimmen:
- die Fensterhöhe
- die Scroll-Abstandswert von dem oberen Rand des Fensters
- den festgelegten oder Zielelement der Abstand von der Oberseite des Fensters
Beachten Sie Folgendes:
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 Demonstration:
Der folgende Code-Schnipsel zeigt ein Ausführungsbeispiel eines gemeinsamen Problems: ein Element an das Ansichtsfenster, wenn ein bestimmtes Element blättert in dem Blick zu fixieren.
/* 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 Beispiel:CodePen
HINWEIS:
Dies gilt nur, Aktionen auf dem Dokument scrollen und nicht an verschachtelte Elemente innerhalb eines Scroll-Ansichtsbereich enthalten .
duplizieren? http://stackoverflow.com/questions/487073/jquery-check-if-element-is-visible-after-scroling – awshepard
Möglicherweise ein weiteres Duplikat: http://stackoverflow.com/questions/1225102/jquery-event-to- trigger-action-when-a-div-is-made-visible –