当浏览器滚动到元素时,更改它的CSS [英] Change a element's CSS when browser scrolls to it
问题描述
我想在浏览器滚动到元素或浏览器触摸它时更改它的CSS.
I want to change an element's CSS as soon as the browser scrolls to it or when the browser touch it.
这是一张有助于更好地说明它的图像:
Here's an image that'll help explain it better:
图片http://i282.photobucket. com/albums/kk246/leeV18/publicweb/jquery-help-1_zpscba356f6.png 像示例图片中一样,我想在浏览器滚动或触摸div时立即更改div的背景.
Image http://i282.photobucket.com/albums/kk246/leeV18/publicweb/jquery-help-1_zpscba356f6.png Like in the example image, I want to change the background of the div as soon as the web browser scrolls to it or touch it.
我认为您可以使用jquery来做到这一点,但是我是一个新手.请帮助,如果有人可以,谢谢.
I think you can do this with jquery, but I'm a total newbie at it. Please help if anyone can, thank you.
推荐答案
这是您的解决方案
http://jsfiddle.net/hushme/VF46X/
$(window).scroll(function(){
$('.hide').each(function(){
var h = $(window).scrollTop();
var o = $(this).offset().top;
var height = $(this).outerHeight();
var t = o-(height*1.3);
if (h >= t) {
$(this) .css('background','green')
}else {$(this) .css('background','blue')}
});
});
这篇关于当浏览器滚动到元素时,更改它的CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!