我需要在滚动时更改 class(backwhitemarq) 的背景颜色,并且 class backwhitemarq 必须固定位置吗?
html 代码:
<div class="backwhitemarq">
/*i need to change the background color for this class.*/
<center> offer of the day</center>
</div>
css 代码:
.backwhitemarq
{
background-color: #f6e454;
padding: 3px;
border-radius: 3px;
height: 44px;
margin-top: 15px;
position:fixed; //fixed
width:100%;
z-index:100;
}
.spacewww /*another class for background color*/
{
background-color:green;
}
javascript 代码:
<script type="text/javascript">
$(window).scroll(function() /*scroll function for window*/
{
var speedscr =$('backwhitemarq'),
scroll = $(window).scrollTop();
if (scroll >= 10)
{
speedscr.addClass("spacewww");
} else
{
speedscr.removeClass("spacewww");
}
});
</script>
最佳答案
$(window).scroll(function() /*scroll function for window*/
{
var speedscr =$('.backwhitemarq');
var scroll = $(window).scrollTop();
if (scroll >= 10)
{
speedscr.addClass("spacewww");
} else
{
speedscr.removeClass("spacewww");
}
});
body{ height: 10000px; }
.backwhitemarq
{
background-color: #f6e454;
padding: 3px;
border-radius: 3px;
height: 44px;
margin-top: 15px;
position:fixed; //fixed
width:100%;
z-index:100;
}
.spacewww /*another class for background color*/
{
background-color:green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="backwhitemarq">/*i need to change the background color for this class.*/
<center> offer of the day</center>
</div>
我想你忘了在 jquery 选择器中添加 .(dot)
<script type="text/javascript">
$(window).scroll(function() /*scroll function for window*/
{
var speedscr =$('.backwhitemarq'),
scroll = $(window).scrollTop();
if (scroll >= 10)
{
speedscr.addClass("spacewww");
} else
{
speedscr.removeClass("spacewww");
}
});
</script>
关于javascript - 滚动时设置类的背景颜色,并且类必须在固定位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39178474/