JS实现滚动条滚动到指定位置时,更换标签样式实现固定效果

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/PrisonersDilemma/article/details/90168759

效果描述:
当页面滚动卷去一个可视高度后,
通过header标签增加class名‘top-fixed’来控制样式。

	$(window).on('scroll',()=>{
		let $fixedheader = $('header'); // fixed容器
		// console.log(fixedheader);
		var wintop=$(window).scrollTop(); // 已滚动卷去的高度
		// console.log(wintop);
		let winHeight = $(window).height(); // 可视窗口的高度
		// console.log(winHeight);
		// 卷去一个可视窗口高度后执行
		if (wintop - winHeight > 0) {
			// fixedheader.hide();
			$fixedheader.addClass("top-fixed");
		} else {
			// fixedheader.show();
			$fixedheader.removeClass("top-fixed");
		}
	})
/* 固定顶部 */
		header{
			height:114px;
		}
		.top-fixed{
			position:fixed;
			animation:mymove .5s forwards; 
			z-index:10;
		}
		@keyframes mymove{
			from {
				top:-114px;
			}
			to {
				top:0px;
			}
		}

猜你喜欢

转载自blog.csdn.net/PrisonersDilemma/article/details/90168759