问题
Please see this:
http://jsfiddle.net/5Zs6F/2/
As you can see only when you scroll past the first red rectangle it turns blue, I would like it to turn blue the moment it enters into view. This is why the second never turns blue because there isn't enough content underneath it to allow you to scroll past it.
HTML:
Scoll this window pane
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div class="box"></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div class="box"></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
CSS:
.box { width: 250px; height: 100px; border: 2px solid red; }
jQuery:
$.getScript('http://imakewebthings.com/jquery-waypoints/waypoints.min.js', function() {
$('.box').waypoint(function() {
$(this).css({
borderColor: 'blue'
});
});
});
How to make it fire as soon as the element in question is seen and not scrolled past?
回答1:
The offset
option determines where in relation to the top of the viewport the waypoint should fire. By default it is 0, so your element fires when it hits the top. Because what you want is common, waypoints includes a simple alias for setting the offset to fire when the whole element comes into view.
$('.box').waypoint(function() {
$(this).css({
borderColor: 'blue'
});
}, { offset: 'bottom-in-view' });
If you want it to fire when any part of the element peeks in from the bottom, you should set it to '100%'.
回答2:
Won't work for me. I have serveral classes with the same name and they will all changed if the page loads / first element is on screen.
jQuery(document).ready(function(){
jQuery('.zoomInDownInaktiv').waypoint(function() {
//jQuery(this).removeClass('zoomInDownInaktiv');
jQuery(this).addClass('zoomInDown');
}, { offset: 'bottom-in-view' });
});
回答3:
OK. Found a solution which works fine.
jQuery('.zoomInDownInaktiv').waypoint(function(direction) {
if (direction === "down") {
jQuery(this.element).removeClass('zoomInDownInaktiv');
jQuery(this.element).addClass('zoomInDown');
}
}, { offset: '80%' });
来源:https://stackoverflow.com/questions/12497122/how-to-make-jquery-waypoints-plugin-fire-when-an-element-is-in-view-and-not-scro