欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jquery根据锚点offset值实现动画切换

程序员文章站 2022-06-20 09:30:03
锚点相信大家都使用过吧!点击后僵硬的切换是不是很不爽呢? 下面分享一个小技巧,根据锚点offset值来实现动画切换

锚点相信大家都使用过吧!点击后僵硬的切换是不是很不爽呢?

下面分享一个小技巧,根据锚点offset值来实现动画切换

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<style>
*{ margin:0; padding:0;}
.main{ width:1000px; margin:0 auto; position:relative; }
.main img{ float:left;}

.bg{ position:absolute; width:100%; z-index:-5;}
.bg01{ height:885px;background:url(../images/b_01.jpg) no-repeat center;}
.bg02{ height:938px;background:url(../images/b_04.jpg) no-repeat center; }
.bg03{ height:946px;background:url(../images/b_02.jpg) no-repeat center; }
.bg04{ height:946px;background:url(../images/b_03.jpg) no-repeat center; }

.p_scoll{ position:fixed; width:50px; height:200px; right:100px; top:50%;}
a{ display:block; background:#3f6; color:#000; width:50px; height:50px; }
</style>
</head>

<body>
<p class="wrapper">
<p class="main">
<p id="tab_01" class="tab"><img src="images/m_01.jpg" alt="" /></p>
<p id="tab_02" class="tab"><img src="images/m_04.jpg" alt="" /></p>
<p id="tab_03" class="tab"><img src="images/m_02.jpg" alt="" /></p>
<p id="tab_04" class="tab"><img src="images/m_03.jpg" alt="" /></p>
</p> 
<p class="bg"> 
<p class="bg01" id="bg01"></p> 
<p class="bg02" id="bg02"></p> 
<p class="bg03" id="bg03"></p> 
<p class="bg04" id="bg04"></p> 
</p> 
</p>
<p class="p_scoll">
<a href="#bg01">图1</a>
<a href="#bg02">图2</a>
<a href="#bg03">图3</a>
<a href="#bg04">图4</a>
</p>
<script>
$(function(){
$(".p_scoll a").click(function(){
$("html,body").animate({ scrolltop:$($(this).attr("href")).offset().top + "px"},1000);
});
});

</script>
</body>
</html>