gpt4 book ai didi

jquery - 使用 jQuery 突出显示位于窗口中心的唯一 div

转载 作者:太空宇宙 更新时间:2023-11-04 15:19:24 25 4
gpt4 key购买 nike

我有几个 id="scroll_1"、"scroll_2"、"scroll_3"等的 div...当这些 div 中的任何一个位于窗口中央时,我想使用 jQuery 突出显示和/或更改位于窗口中央的任何一个 div 的背景颜色。目前,背景颜色一旦位于屏幕中央就会发生变化,但一旦它不再位于中心(即用户向下/向上滚动到另一个 scroll_x id),我就无法将其改回原始背景颜色。

编辑 我唯一相关的 css 代码是:

[id^=scroll_]{
background-color:white;
}

感谢您的帮助!

<script>
$(document).ready(function() {
var window_height = $(window).height();
var obj_height = $('#scroll_1').height(); //height of object we are scrolling past
var top = $('#replyer').offset().top + (obj_height /2); //position on screen to start highlighting #scroll_x

$(window).scroll(function() {
var scrollMiddle = $(window).scrollTop() + ((window_height/2) - (obj_height /2));

if (scrollMiddle >= top) {
var scrollPosition = $(document).scrollTop()+ ((window_height/2) - (obj_height /2)),
currentPosition = 0;
$('div[id^="scroll_"]').each(function() {//iterate over #scroll_x and only change background until another #scroll_x is in the middle of the screen
currentPosition = $(this).offset().top;
if (currentPosition >= scrollPosition) {
$(this).prev(function(){
$(this).css('background-color',"#aaa"); //change previous #scroll_x back to original background color - Not Working Currently
});

return false; // break the loop
}

$(this).css('background-color',"#ccc"); //currently changes background of #scroll_x once in middle of screen but stays highlighted when scrolling up/down to previous/next iteration of #scroll_x
});
}
});
});
</script>

HTML:

<div id="replyer">
Top line before repeating divs
</div>
<div id="scroll_1">
First object to scroll over.
</div>
<div id="scroll_2">
Want to highlight div currently in the middle of screen
</div>
<div id="scroll_3">
Only div in middle of screen should be highlighted (background change)
</div>

最佳答案

我不确定这是否正是您所追求的,但这里有一个演示,可以将与浏览器中间重叠的对象更改为绿色。

fiddle :http://jsfiddle.net/j2ULW/1/

完整来源:

<html>
<head>
<title>Scroll test</title>
</head>
<style type="text/css">
body {
background: #000;
}
[id^=scroll_]{
background-color:#aaa;
height: 600px;
}
#replyer {
height: 400px;
background: white;
}
</style>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<body>
<div id="replyer">
Top line before repeating divs
</div>
<div id="scroll_1">
First object to scroll over.
</div>
<div id="scroll_2">
Want to highlight div currently in the middle of screen
</div>
<div id="scroll_3">
Only div in middle of screen should be highlighted (background change)
</div>
<script>
$(document).ready(function() {
var window_height = $(window).height();
$(window).scroll(function() {
var scrollMiddle = $(window).scrollTop() + (window_height/2);
$('div[id^="scroll_"]').each(function() {
elTop = $(this).offset().top;
elBtm = elTop + $(this).height();
if (elTop < scrollMiddle && elBtm > scrollMiddle) {
$(this).css('background-color',"#00ff00");
} else {
$(this).css('background-color',"#aaa");
}
});
});
});
</script>
</body>
</html>

关于jquery - 使用 jQuery 突出显示位于窗口中心的唯一 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10992677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com