模仿淘宝侧边栏的滚动条

<body>
	<div class="title">
		<ul>
			<li class="active" title-id="box01">我常逛的</li>
			<li title-id="box02">时尚</li>
			<li title-id="box03">品质</li>
			<li title-id="box04">特色</li>
			<li title-id="box05">实惠</li>
			<li title-id="box06">猜你喜欢</li>
			<li style="display: none">顶部</li>
		</ul>
	</div>
	<div class="box" id="box01">
		<h2>我常逛的</h2>
	</div>
	<div class="box" id="box02">
		<h2>时尚</h2>
	</div>
	<div class="box" id="box03">
		<h2>品质</h2>
	</div>
	<div class="box" id="box04">
		<h2>特色</h2>
	</div>
	<div class="box" id="box05">
		<h2>实惠</h2>
	</div>
	<div class="box" id="box06">
		<h2>猜你喜欢</h2>
	</div>

</body>

  

  

<style>
		* {
			margin: 0;
			padding: 0;
		}
		.box {
			height: 800px;
			width: 800px;
			border: 1px solid red;
			margin: 0 auto;
		}
		.box h2 {
			text-align: center;
		}
		ul {
			position: absolute;
			top: 300px;
			right: 0;
		}
		ul li {
			width: 40px;
			text-align: center;
			list-style: none;
			border-bottom: 1px solid #efefef;
			cursor: pointer;
			padding: 10px;
			transition: 0.3s;
		}
		.title li:nth-child(1) { color: #f40; }
		.title li:nth-child(2) { color: #f05; }
		.title li:nth-child(3) { color: #8d7afb; }
		.title li:nth-child(4) { color: #a8c001; }
		.title li:nth-child(5) { color: #a2745b; }
		.title li:nth-child(6) { color: #f40; }
		.title li:nth-child(1).active, .title li:nth-child(1):hover { background: #f40; color: white; }
		.title li:nth-child(2).active, .title li:nth-child(2):hover { background: #f05; color: white; }
		.title li:nth-child(3).active, .title li:nth-child(3):hover { background: #8d7afb; color: white; }
		.title li:nth-child(4).active, .title li:nth-child(4):hover { background: #a8c001; color: white; }
		.title li:nth-child(5).active, .title li:nth-child(5):hover { background: #a2745b; color: white; }
		.title li:nth-child(6).active, .title li:nth-child(6):hover { background: #f40; color: white; }
		.title li:nth-child(7).active, .title li:nth-child(7):hover { background: #f40; color: white; }

	</style>

  

<script>
$( function()
{
	/**
	 * 滚动监听事件
	 */
	$( window ).scroll( function()
	{

		var scrollTop = $( window ).scrollTop();
		//console.log(scrollTop)

		//遍历每个box模块
		$( ".box" ).each( function( i, e )
		{
			//当前的元素相对于文档的top与页面的滚动高度(scrollTop)
			if( scrollTop >= $( e ).offset().top )
			{
				// 获取当前box的ID
				var id = $( e ).attr( "id" );
				//通过属性选择器获取当前模块对应的菜单
				$( "[title-id=" + id + "]" ).addClass( "active" );
				$( "[title-id=" + id + "]" ).siblings().removeClass( "active" )
			}
		} );

		if( scrollTop >= 290 )
		{
			$( "ul" ).css( {
				"position": "fixed",
				"top": 10
			} );

		}
		else
		{
			$( "ul" ).css( {
				"position": "absolute",
				"top": 300
			} );

		}
		if( scrollTop >= 330 )
		{
			$( ".title ul li:nth-child(7)" ).css( "display", "block" );
		}
		else
		{
			$( ".title ul li:nth-child(7)" ).css( "display", "none" );
		}
	} );
	setTimeout( function()
	{
		$( window ).scrollTop( 0 );
	}, 10 );

	$( ".title li" ).click( function()
	{
		var id = $( this ).attr( "title-id" );
		var top = 0;
		if( id != "" && id != undefined )
		{
			top = $( "#" + id ).offset().top;
		}

		//$(window ).scrollTop(top);
		$( "html,body" ).animate( {
			"scrollTop": top
		}, 500 );
	} );

} )

</script>

  

转载于:https://www.cnblogs.com/yuanyanbk/p/8399052.html

### Vue 项目中隐藏侧边栏滚动条的 CSS 解决方案 在 Vue 项目中,为了实现隐藏侧边栏 (`sidebar`) 的滚动条效果,可以通过应用特定的 CSS 属性来达到目的。通常情况下,这涉及到设置 `overflow` 和自定义滚动条样式。 对于现代浏览器而言,可以利用 `-webkit-scrollbar` 来控制滚动条的表现形式: ```css /* 隐藏整个页面的滚动条 */ html { scrollbar-width: none; /* Firefox */ } /* 对于 WebKit 浏览器 (Chrome, Safari) */ .sidebar::-webkit-scrollbar { display: none; } ``` 上述代码片段通过将 `.sidebar` 类下的滚动条显示属性设为 `none` 实现了视觉上的隐藏效果[^1]。需要注意的是,在不同类型的浏览器上可能需要额外处理以确保兼容性;例如针对火狐浏览器使用 `scrollbar-width: none;` 可以有效去除默认滚动条。 另外一种方式则是直接操作容器本身的溢出行为: ```css .sidebar { overflow-y: auto; /* 当内容超出时允许纵向滚动 */ -ms-overflow-style: none; /* Internet Explorer 10+ */ scrollbar-width: none; /* Firefox */ } .sidebar::-webkit-scrollbar { width: 0 !important; /* Chrome/Safari/Webkit */ } ``` 此方法不仅能够移除可见的滚动轨道,同时也保留了实际的内容可滚特性,即用户仍然可以通过鼠标滚轮或其他输入设备浏览被遮挡的部分内容。 最后值得注意的一点是在某些场景下,如果希望彻底禁用滚动功能,则可以直接把 `overflow` 设置成 `hidden`: ```css .sidebar { overflow: hidden; } ``` 这种方式会完全阻止任何方向上的滚动动作发生,适用于不需要内部滚动的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值