jquery简单导航栏

图示:


html代码:

<ul class="navlist">
		<li class="nav">1</li>
		<li class="nav">2</li>
		<li class="nav">3</li>
		<li class="nav">4</li>
		<li class="nav">5</li>
	</ul>
	<div class="big_wrap">
		<div class="div">1</div>
		<div class="div">2</div>
		<div class="div">3</div>
		<div class="div">4</div>
		<div class="div">5</div>
	</div>

	<div class="wenzi"></div>

css代码:

ul{
		width: 1000px;
		height: 100px;
		display: block;
	}
	
	.nav{
		width: 200px;
		height: 100px;
		background: #ccc;
		display: block;
		float: left;
		border: 1px solid #fff;
		text-align: center;
		line-height: 100px;
		
	}

	.active{
		color: #fff;
		background: pink;
	}

	.big_wrap{
		position: relative;
	}
	.div{
		width: 1000px;
		height: 300px;
		background:#ccc;
		display: none;
		position: absolute;
		top: 0;
		left: 0;
	}
	.wenzi{
		width: 100%;
		height: 500px;
		background: red;
	}

js代码:

$('.navlist .nav:first').addClass('active')     //默认设置第一项为当前样式

      $('.navlist .nav').on('mouseenter',function(){
      	//获取当前操作下标
      	var index=$(this).index();     

      	$(this).addClass('active');                   //鼠标移入项设置为当前样式并让自己的兄弟节点取消当前样式
      	$(this).siblings().removeClass('active');  
      	$('.big_wrap .div').eq(index).show();
      	$('.big_wrap .div').eq(index).siblings().hide();

      })

       $('.navlist .nav').on('mouseout',function(){
       	var index=$('.navlist .nav').index(this);     
       	$('.big_wrap .div').eq(index).hide();       		       	     	
      })

      $('.big_wrap .div').on('mouseenter',function(){
      	$(this).show();
      })

      $('.big_wrap .div').on('mouseout',function(){
      	$(this).hide();
      	
      })

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单jQuery 滑动导航条的示例: HTML 代码: ``` <nav> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> <li><a href="#section3">Section 3</a></li> </ul> </nav> <section id="section1"> <h2>Section 1</h2> <p>Content for section 1 goes here.</p> </section> <section id="section2"> <h2>Section 2</h2> <p>Content for section 2 goes here.</p> </section> <section id="section3"> <h2>Section 3</h2> <p>Content for section 3 goes here.</p> </section> ``` CSS 代码: ``` nav { position: fixed; top: 0; left: 0; width: 100%; background-color: #333; color: #fff; } nav ul { margin: 0; padding: 0; list-style: none; display: flex; justify-content: space-around; } nav ul li { margin: 0 10px; } nav ul li a { display: block; padding: 10px; text-decoration: none; color: #fff; } nav ul li a:hover { background-color: #555; } ``` jQuery 代码: ``` $(document).ready(function() { $('nav ul li a').click(function(e) { e.preventDefault(); // 阻止默认行为 var target = $(this).attr('href'); // 获取目标元素的 ID $('html, body').animate({ scrollTop: $(target).offset().top // 滑动到目标元素的位置 }, 1000); // 滑动时间为 1 秒 }); }); ``` 解释: 1. 在 HTML 中,我们创建了一个带有三个部分的导航条和三个部分的页面内容。每个部分都有一个唯一的 ID,可以通过链接中的 # 符号进行访问。 2. 在 CSS 中,我们创建了一个固定在页面顶部的导航条。 3. 在 jQuery 中,我们使用了 `.click()` 方法来监听导航链接的点击事件。当链接被点击时,我们使用 `.attr()` 方法获取目标元素的 ID,并使用 `.offset().top` 方法获取目标元素相对于文档顶部的位置。然后,我们使用 `.animate()` 方法滑动页面到目标位置。滑动时间为 1 秒。 4. 通过以上代码,当用户点击导航链接时,页面会平滑滑动到对应的部分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值