如何改变颜色时,用户滚动
How to change color when user scrolls
我有一段jQuery代码,当用户向下滚动时,它会改变几个颜色的元素。但是当用户向上滚动页面时,我想切换到原始颜色。它不像我期望的那样工作…
原始代码
jQuery(window).scroll(function()
{
var scrollTop = jQuery(this).scrollTop(),
offset = jQuery('#masthead').height() - 55;
if ( scrollTop < offset )
jQuery(".float-manu").css({ backgroundColor: "rgba(0,0,0,0)" });
else
jQuery(".float-manu").css({ backgroundColor: "rgba(0,0,0,0.7)" });
})
修改后的代码也可以工作
})(window.jQuery);
jQuery(window).scroll(function()
{
var scrollTop = jQuery(this).scrollTop(),
offset = jQuery('#masthead').height() - 55;
if ( scrollTop < offset )
jQuery(".float-manu").css({ backgroundColor: "rgba(0,0,0,0)" });
else
jQuery(".float-manu").css({ backgroundColor: "rgba(0,0,0,0.7)" });
jQuery(".primary-menu a").css({ color: "white" });
})
添加额外的css修饰符到第一个if语句终止脚本
})(window.jQuery);
jQuery(window).scroll(function()
{
var scrollTop = jQuery(this).scrollTop(),
offset = jQuery('#masthead').height() - 55;
if ( scrollTop < offset )
jQuery(".float-manu").css({ backgroundColor: "rgba(0,0,0,0)" });
jQuery(".primary-menu a").css({ color: "black" });
else
jQuery(".float-manu").css({ backgroundColor: "rgba(0,0,0,0.7)" });
jQuery(".primary-menu a").css({ color: "white" });
})
我看到您在if
和else
中缺少大括号。因此,只执行if
和else
后面的第一行。而是添加一个括号,像这样:
.....
if ( scrollTop < offset ) {
jQuery(".float-manu").css({ backgroundColor: "rgba(0,0,0,0)" });
jQuery(".primary-menu a").css({ color: "black" });
}
else {
jQuery(".float-manu").css({ backgroundColor: "rgba(0,0,0,0.7)" });
jQuery(".primary-menu a").css({ color: "white" });
}
....
相关文章:
- 当用户滚动到页面的某个部分时,如何触发关键帧
- 下一个/上一个锚链接,如何在用户滚动时更新下一个/上一个链接
- 在用户滚动时加载单个页面网站的不同部分
- 当用户滚动到底部时,Ajax函数会多次触发
- jQuery查找最大用户滚动值
- 每当用户滚动到Chart.js时,它的大小就会翻倍
- 通过用户滚动量进行检测
- 当用户滚动到 iframe 或 iframe 位于视口中时,从 iframe 调用 js 函数
- 如何在用户滚动到页面底部时仅加载一次数据
- 如何在用户滚动页面时缓慢移动标题
- 如何确保用户滚动并阅读使用条款
- JavaScript:当用户滚动时对滚动进行动画处理
- 在用户滚动时创建 JavaScript 弹出框
- 当用户滚动到特定元素的末尾时,如何运行jQuery代码
- JS:防止用户滚动的默认?或者强制 GSAP 在用户上方滚动
- 为什么在用户滚动后,设置超时不会在触摸端处理程序中触发(iOS Safari)
- Don't加载图像,直到用户滚动到那里(可能)
- 当用户滚动到页面底部时显示页脚
- 如何在用户滚动时更新谷歌地图中心
- 当用户滚动jquery时,停止ScrollTop功能