需要实现的效果

样式分析:

2个主要部分,头部的标题和导航部分,和主要的功能实现区域;

1.头部

    <div id="header">
<h1>动漫视频</h1>
<span><</span>
<span>></span>
</div>
<div id="tips">
<span class="on"> </span>
<span> </span>
<span> </span>
</div>

2.功能区域

    <div id="viewBox">
<ul id="contentBox">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>11</li>
<li>12</li>
</ul>
</div>

添加样式

    <style>
#header{
display: inline-block;
}
#header h1{
display: inline-block;
font-size: 32px;
padding-left: 20px;
}
#header span{
width: 30px;
height: 30px;
display: inline-block;
font-size: 23px;
cursor:pointer;
border: 1px solid #eee;
border-radius: 3px;
text-align: center;
}
#header span:hover{
background: #333;
color: #fff;
} #viewBox{
width: 960px;
height: 140px;
font-size: 0;
overflow: hidden;
border: 1px solid #333;
}
#contentBox{
width: 2880px;
position: relative;
}
#contentBox>li{
width: 200px;
height: 100px;
background: pink;
display: inline-block;
font-size: 12px;
margin: 20px;
}
#tips{
display: inline-block;
}
#tips span{
display: inline-block;
width: 20px;
height: 20px;
border-radius: 20px;
text-align: center;
line-height: 20px;
border: 1px solid #eee;
cursor: pointer;
}
#tips span:hover{
background: #999;
color: white;
}
#tips span.on{
background: black;
color: white;
}
</style>

此处需要注意,因为需要给 contentBox 添加 animate left 方法,所以需要给它的 position : related 才能使 animate left 生效

功能分析:

1.下一页

2.当下一页滚动到最后一页面,再点击下一页的时候,需要返回到第一页

3.上一页

4.当上一页滚动到第一页,再点击上一页的时候,需要前进到最后一页

5.当点击圆圈的时候,前进到任意一页

功能实现

首先,我们在接下来的功能中常用的一些元素,先赋值给变量

$vBox = $('#viewBox');                          /* 可以见到的展示区域 */
$cBox = $('#contentBox'); /* 内容区域 */
vWidth = $vBox.width(); /* 可见区域的宽度 */
cWidth = $cBox.width(); /* 内容区域的宽度 */

然后,我们有5个小功能,下一页(goNext),回到顶部(goTop),上一页(goBack),回到底部(goBotoom),到任意页(goPage)

1.下一页(goNext)

var vLeft=$cBox.position().left;                //内容区域距左侧的距离
$cBox.animate({left: '-='+vWidth+'px'});

2.回到顶部(goTop)

var vLeft=$cBox.position().left;
$cBox.animate({left: 0});

3.上一页(goBack)

var vLeft=$cBox.position().left;                //内容区域距左侧的距离
$cBox.animate({left: '+='+vWidth+'px'});

4.回到底部(goBotoom)

var vLeft=$cBox.position().left;                //内容区域距左侧的距离
$cBox.animate({left: -cWidth+vWidth});

5.到任意页(goPage)

var vLeft=$cBox.position().left;                //内容区域距左侧的距离
$cBox.animate({left: -vWidth*page}); //传递一个page参数用来知道需要跳转到第几页

当每个小功能实现后,组合下功能,并绑定功能

var $vBox;
var $cBox;
var vWidth;
var cWidth;
$(function () {
$vBox = $('#viewBox');
$cBox = $('#contentBox');
vWidth = $vBox.width();
cWidth = $cBox.width(); $('#header span:last-child').click(function () {
go('next');
})
$('#header span:nth-child(2)').click(function () {
go('back');
}) $('#tips>span').click(function () {
var $ThisS=$(this);
$ThisS.addClass('on').siblings().removeClass();
go($ThisS.index());
}) }) function go(page) {
var vLeft=$cBox.position().left;
if (!$cBox.is(':animated')){
switch (page){
case 'next':
if( vLeft > -cWidth-vLeft ){
$cBox.animate({left: '-='+vWidth+'px'});
}else{
go('top');
}
break; case 'back':
if( vLeft < 0){
$cBox.animate({left: '+='+vWidth+'px'});
}else{
go('bottom');
}
break; case 'top':
$cBox.animate({left: 0});
break; case 'bottom':
$cBox.animate({left: -cWidth+vWidth});
break; default:
$cBox.animate({left: -vWidth*page});
break;
}
}
}

下面是全部代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>第一个页面</title>
<!--<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">-->
<script src="common/js/jquery-2.1.1.min.js"></script>
<!--<script src="common/js/screenAdaptation.js"></script>-->
<script src="js/download.js"></script>
<script src="js/stopXBack.js"></script>
<link rel="stylesheet" href="common/css/common-style.css?v=<%=Math.random()*100 %>">
<link rel="stylesheet" href="css/download.css?v=<%=Math.random()*100 %>">
<style>
#header{
display: inline-block;
}
#header h1{
display: inline-block;
font-size: 32px;
padding-left: 20px;
}
#header span{
width: 30px;
height: 30px;
display: inline-block;
font-size: 23px;
cursor:pointer;
border: 1px solid #eee;
border-radius: 3px;
text-align: center;
}
#header span:hover{
background: #333;
color: #fff;
} #viewBox{
width: 960px;
height: 140px;
font-size: 0;
overflow: hidden;
border: 1px solid #333;
}
#contentBox{
width: 2880px;
position: relative;
}
#contentBox>li{
width: 200px;
height: 100px;
background: pink;
display: inline-block;
font-size: 12px;
margin: 20px;
}
#tips{
display: inline-block;
}
#tips span{
display: inline-block;
width: 20px;
height: 20px;
border-radius: 20px;
text-align: center;
line-height: 20px;
border: 1px solid #eee;
cursor: pointer;
}
#tips span:hover{
background: #999;
color: white;
}
#tips span.on{
background: black;
color: white;
}
</style>
<script>
var $vBox;
var $cBox;
var vWidth;
var cWidth;
$(function () {
$vBox = $('#viewBox');
$cBox = $('#contentBox');
vWidth = $vBox.width();
cWidth = $cBox.width(); $('#header span:last-child').click(function () {
go('next');
})
$('#header span:nth-child(2)').click(function () {
go('back');
}) $('#tips>span').click(function () {
var $ThisS=$(this);
$ThisS.addClass('on').siblings().removeClass();
go($ThisS.index());
}) }) function go(page) {
var vLeft=$cBox.position().left;
if (!$cBox.is(':animated')){
switch (page){
case 'next':
if( vLeft > -cWidth-vLeft ){
$cBox.animate({left: '-='+vWidth+'px'});
}else{
go('top');
}
break; case 'back':
if( vLeft < 0){
$cBox.animate({left: '+='+vWidth+'px'});
}else{
go('bottom');
}
break; case 'top':
$cBox.animate({left: 0});
break; case 'bottom':
$cBox.animate({left: -cWidth+vWidth});
break; default:
$cBox.animate({left: -vWidth*page});
break;
}
}
} </script>
</head>
<body>
<div id="header">
<h1>动漫视频</h1>
<span><</span>
<span>></span>
</div>
<div id="tips">
<span class="on"> </span>
<span> </span>
<span> </span>
</div>
<div id="viewBox">
<ul id="contentBox">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>11</li>
<li>12</li>
</ul>
</div>
</body>
</html>

【jQuery学习日记】jQuery实现滚动动画的更多相关文章

  1. jquery学习笔记---jquery插件开发

    http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html jquery插件开发:http://www.cnblogs.com/damonla ...

  2. (高级篇)jQuery学习之jQuery Ajax用法详解

    jQuery Ajax在web应用开发中很常用,它主要包括有ajax,get,post,load,getscript等等这几种常用无刷新操作方法,下面我来给各位同学介绍介绍. 我们先从最简单的方法看起 ...

  3. JQuery学习笔记---jquery对象和DOM对象的关系

    1.DOM(Document  Object Model,文档对象模型).DOM树 { html (head&&body),  head(meta && title) ...

  4. jQuery学习之jQuery Ajax用法详解

    jQuery Ajax在web应用开发中很常用,它主要包括有ajax,get,post,load,getscript等等这几种常用无刷新操作方法,下面我来给各位同学介绍介绍. 我们先从最简单的方法看起 ...

  5. jquery学习笔记----jquery相关的文档

    http://tool.oschina.net/apidocs/apidoc?api=jquery http://www.w3school.com.cn/jquery/jquery_ref_event ...

  6. jQuery学习之jQuery Ajax用法详解(转)

    [导读] jQuery Ajax在web应用开发中很常用,它主要包括有ajax,get,post,load,getscript等等这几种常用无刷新操作方法,下面我来给各位同学介绍介绍.我们先从最简单的 ...

  7. jQuery学习之jQuery Ajax用法详解(转)

    jQuery Ajax在web应用开发中很常用,它主要包括有ajax,get,post,load,getscript等等这几种常用无刷新操作方法,下面我来给各位同学介绍介绍. 我们先从最简单的方法看起 ...

  8. jquery 学习日记之选择器

    看完Jquery选择器的教程视频后,对jquery的选择器有一定的认识和了解,现整理一下知识: 一.jquery基本选择器,这类比较简单,一笔带过. #id 选择器,是选择  匹配id值中的第一个元素 ...

  9. 【转载】【JQuery学习】jQuery插件开发

    JQuery做得最好的就是他的闭包和扩展性.超级简单的扩展方法,让更多的人可以轻松的开发定制自己的jQuery插件.下面的东西是转载过来当做学习材料的.虽然貌似有点古老,但是jQuery的变更一直都不 ...

随机推荐

  1. [Algo] 73. Combinations Of Coins

    Given a number of different denominations of coins (e.g., 1 cent, 5 cents, 10 cents, 25 cents), get ...

  2. SpringMVC插件安装、环境配置及快速入门_学习笔记

    SpringMVC 是现在广泛应用的框架结构,我也只是一个初学者,一遍学习一遍梳理整合,如有错误,希望大神指点,别误人. MVC :Model-View-Control 框架性质的C 层要完成的主要工 ...

  3. Serverless 的开发者工具建设

    本文将介绍 Serverless 生态下的开发者工具,并简述这些工具是如何贯穿开发.调试.测试和部署的生命周期,提升开发者效率的. 由于 Serverless 平台具备弹性扩缩.免运维.按需付费等特点 ...

  4. 系统学习Javaweb6----JavaScript2

    感想:感觉自己还是只是学到皮毛,仍需继续努力,明天开始需要学习Android和阅读感想的书写. 学习笔记: 2.3.运算符 JavaScript运算符与java运算符基本一致. 这里我们来寻找不同点进 ...

  5. yii执行流程简单介绍

    1. 用户访问 http://www.example.com/index.php?r=post/show&id=1,Web 服务器执行入口脚本 index.php 来处理该请求.  2. 入口 ...

  6. TreeMap简介

    在Map集合框架中,除了HashMap以外,TreeMap也是常用到的集合对象之一.与HashMap相比,TreeMap是一个能比较元素大小的Map集合,会对传入的key进行了大小排序.其中,可以使用 ...

  7. Spring MVC及与structs MVC对比

    一.Spring MVC MVC: Model + View + Controller(数据模型+视图+控制器) 三层架构: Presentation tier + Application tier ...

  8. JavaScript学习总结(五)原型和原型链详解

    转自:http://segmentfault.com/a/1190000000662547 私有变量和函数 在函数内部定义的变量和函数,如果不对外提供接口,外部是无法访问到的,也就是该函数的私有的变量 ...

  9. android greendao的外部封装不太友好。

    https://github.com/greenrobot/greenDAO 下载下官网的示例,有完整的封装版本,但自已封装是碰到很多问题. 因greenDao的Master和Session中很多方法 ...

  10. Linux启动初始化配置文件浅析

    转自:http://blog.51cto.com/19055/1144600 1)/etc/profile   登录时,会执行. 全局(公有)配置,不管是哪个用户,登录时都会读取该文件. (2)/ec ...