菜单实现切换效果

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
    *{margin:0;
     padding:0;
     font-size:13px;
     list-style:none;}

.menu{width:210px;
      margin:50px auto;
      border:1px solid #ccc;}

.menu p{height:25px;
        line-height:25px;
        font-weight:bold;
        background:#eee;
        border-bottom:1px solid #ccc;
        cursor:pointer;
        padding-left:5px;}

.menu div ul{display:none;}

.menu li{height:24px;
         line-height:24px;
         padding-left:5px;}
</style>
	<script type="text/javascript">
		window.onload = function(){
			var menu = document.getElementById("menu");
			var ps = menu.getElementsByTagName("p");
			var uls = menu.getElementsByTagName("ul");
			for(var i = 0;i < ps.length;i++){
				ps[i].id = i;
				ps[i].onclick = function(){
					for(var j = 0;j < uls.length;j++){
						uls[j].style.display = "none";
					}
					uls[this.id].style.display = "block";
				}
			}
		}
	</script>
</head>
<body>
	<div class="menu" id="menu">
		<div>
			<p>Web前端</p>
			<ul style="display:block">
				<li>JavaScript</li>
				<li>DIV+CSS</li>
				<li>jQuery</li>
			</ul>
		</div>
		<div>
			<p>后台脚本</p>
			<ul>
				<li>PHP</li>
				<li>ASP.net</li>
				<li>JSP</li>
			</ul>
		</div>
		<div>
			<p>前端框架</p>
			<ul>
				<li>Extjs</li>
				<li>Esspress</li>
				<li>YUI</li>
			</ul>
		</div>
	</div>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中实现顶部滑动切换菜单效果,可以通过以下步骤实现: 1. 首先,我们需要创建一个Vue组件来表示菜单。可以使用`v-for`指令循环渲染菜单选项,并通过`v-bind`绑定`isActive`属性来控制当前选中的菜单项。同时,设置一个`@click`事件监听器,使得点击菜单项时可以切换选中的项。 2. 在样式上,可以使用CSS来控制菜单项的样式,使得被选中的项可以突出显示。 3. 接下来,我们需要监控滚动事件。可以使用`window`对象的`scroll`事件,并将其绑定到一个滚动事件处理函数上。 4. 在滚动事件处理函数中,通过获取`window.pageYOffset`属性来获取当前滚动的位置。然后,计算出每个菜单项距离顶部的距离,与当前滚动位置进行比较。以此来判断当前应该激活哪个菜单项。 5. 最后,通过将`isActive`属性绑定到菜单项的样式上,以实现滚动切换菜单效果。 综上所述,以上步骤可以实现Vue中顶部滑动切换菜单效果实现。具体代码请参考以下示例: ``` <template> <div> <ul> <li v-for="(item, index) in menuItems" :key="index" :class="{ active: item.isActive }" @click="setActive(index)"> {{ item.title }} </li> </ul> </div> </template> <script> export default { data() { return { menuItems: [ { title: '菜单1', isActive: false }, { title: '菜单2', isActive: false }, { title: '菜单3', isActive: false }, ], } }, mounted() { window.addEventListener('scroll', this.handleScroll) }, destroyed() { window.removeEventListener('scroll', this.handleScroll) }, methods: { setActive(index) { this.menuItems.forEach((item, i) => { item.isActive = i === index }) }, handleScroll() { const scrollY = window.pageYOffset const menuItems = this.$refs.menuItems menuItems.forEach((item, index) => { const rect = item.getBoundingClientRect() const offsetTop = rect.top + scrollY this.menuItems[index].isActive = scrollY >= offsetTop && scrollY < offsetTop + rect.height }) }, }, } </script> <style> .active { /* 设置选中菜单项的样式 */ background-color: #ccc; } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值