关于使用JS做轮播图,使用一个章节进行笔迹。

一:简单轮播图

1.程序

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
border: 0;
}
.all {
width: 500px;
height: 200px;
padding: 7px;
border: 1px solid #ccc;
margin: 100px auto;
position: relative; /*主要用于左右箭头的定位*/
}
.screen {
width: 500px;
height: 200px;
overflow: hidden;
position: relative;
}
.screen ul {
position: absolute;
left: 0;
top: 0;
width: 3000px; /*将图片都装进来*/
}
.screen li {
float: left;
width: 500px;
height: 200px;
}
/*-序号-*/
.screen ol {
position: absolute;
left: 50%;
bottom: 10px;
line-height: 20px;
text-align: center;
}
.screen ol li {
float: left;
width: 20px;
height: 20px;
background-color: #fff;
border: 1px solid #ccc;
margin-left: 10px;
cursor: pointer;
border-radius: 5px;
}
.screen ol li.current {
background-color: #DB192A;
}
#arr span {
width: 40px;
height: 40px;
position: absolute;
left: 5px;
top: 50%;
margin-top: -20px;
background: #000;
cursor: pointer;
line-height: 40px;
text-align: center;
font-family: "黑体";
font-size: 30px;
font-weight: bold;
color: #fff;
opacity: 0.3;
border: 1px solid #ffffff;
}
#arr #right {
right: 5px;
left: auto;
}
#arr {
display: none;
}
</style>
</head>
<body>
<div id="box" class="all">
<div class="screen">
<ul>
<li><img src="data:images/1.jpg" alt="" width="500" height="200"></li>
<li><img src="data:images/2.jpg" alt="" width="500" height="200"></li>
<li><img src="data:images/3.jpg" alt="" width="500" height="200"></li>
<li><img src="data:images/4.jpg" alt="" width="500" height="200"></li>
<li><img src="data:images/5.jpg" alt="" width="500" height="200"></li>
</ul>
<ol>
<!--序号-->
</ol>
</div>
<div id="arr">
<span id="left">&lt;</span>
<span id="right">&gt;</span>
</div>
</div> <script>
//获取一些使用的数据
var box = document.getElementById("box");
var screenObj = box.children[0];
var imgWidth = screenObj.offsetWidth;
var ulObj = screenObj.children[0];
var listObj = ulObj.children;
var olObj = screenObj.children[1];
var arr = document.getElementById("arr"); //同步全局变量
var pic = 0; for(var i=0; i<listObj.length; i++){
//创建ol中的li
var li = document.createElement("li");
olObj.appendChild(li);
li.innerHTML=i+1;
li.setAttribute("index",i);
//注册鼠标进入事件
li.onmouseover=function () {
for (var j=0; j<olObj.children.length; j++){
olObj.children[j].removeAttribute("class");
}
this.className="current";
pic = this.getAttribute("index");
animate(ulObj, -pic * imgWidth); //移动图片
}
}
olObj.children[0].className="current"; //刚进入的初始化
ulObj.appendChild(ulObj.children[0].cloneNode(true)); //在后面再添加一张第一张的图片 //进行自动播放轮播
var timeId = setInterval(clickHandle, 200000000); //鼠标进入div,显示左右箭头,停止定时器
box.onmouseover=function () {
arr.style.display="block";
clearInterval(timeId);
}
//鼠标退出div,影藏左右箭头,继续定时器
box.onmouseout=function () {
arr.style.display="none";
timeId = setInterval(clickHandle, 2000);
} function clickHandle() {
if(pic==listObj.length-1){
pic=0;
ulObj.style.left=0+"px";
}
pic++;
animate(ulObj, -pic * imgWidth);
if(pic==listObj.length-1){
olObj.children[olObj.children.length-1].className="";
olObj.children[0].className="current";
}else{
for(var k=0;k<olObj.children.length; k++){
olObj.children[k].removeAttribute("class");
}
olObj.children[pic].className="current";
}
} //右边按钮
document.getElementById("right").onclick=clickHandle; //左边按钮
document.getElementById("left").onclick=function () {
if(pic==0){
pic=5;
ulObj.style.left=-pic * imgWidth + "px";
}
pic--;
animate(ulObj, -pic * imgWidth);
for(var v=0; v<olObj.children.length; v++){
olObj.children[v].removeAttribute("class");
}
olObj.children[pic].className="current";
} //移动到任意位置
function animate(ele, target) {
clearInterval(ele.timeId);
ele.timeId = setInterval(function () {
var current = ele.offsetLeft;
var step = 10;
step = current<target?step:-step;
current+=step;
if(Math.abs(current-target)>Math.abs(step)){
ele.style.left=current+"px";
}else{
clearInterval(ele.timeId);
ele.style.left=target+"px";
}
},10);
} </script>
</body>
</html>

2.效果:

  

二:offset系列

1..offsetLeft

  没有脱离文档流:

    父级元素margin+父级元素的padding+父级元素的border+自己的margin。

  脱离文档流:

    主要是自己的left+自己的margin

三:document获取元素

1.通过document获取元素

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
//获取body
console.log(document.body);
//获取title
console.log(document.title);
//获取html
console.log(document.documentElement);
</script>
</body>
</html>

2.图片跟着鼠标飞

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
img {
position: absolute;
}
</style>
</head>
<body>
<img src="demo/image/00_7.jpg" alt="" id="img" width="300px">
<script>
document.onmousemove=function (e) {
document.getElementById("img").style.left=e.clientX+"px";
document.getElementById("img").style.top=e.clientY+"px";
}
</script>
</body>
</html>

009 轮播图,offset系列的更多相关文章

  1. JavaScript动画:offset家族和匀速动画详解(含轮播图的实现)

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. offset家族简介 我们知道,三大家族包括:offset/scroll ...

  2. JavaScript动画:offset和匀速动画详解(含轮播图的实现)

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. offset简介 我们知道,三大家族包括:offset/scroll/c ...

  3. js-BOM之offset家族、移动函数的封装升级(轮播图)

    Obj.style.width/obj.style.height与obj.offsetWidth/obj.offsetHeight的区别: <style> #div1{ height: 2 ...

  4. JS-特效 ~ 01. 事件对象、offset偏移/检测、无缝滚动、自动循环轮播图

    Math.round ( ) :正书四舍五入,负数五舍六入 用定时器,先清除定时器 事件对象 event event:事件被触动时,鼠标和键盘的状态,通过属性控制 Offset:偏移,检测 1. 获取 ...

  5. jQuery实现todo及轮播图

    内容: 1.todo程序 2.轮播图 1.todo程序 需求: 实现一个todo程序,可以添加数据,可以删除数据,可以修改数据,可以查看所有数据 另外实现自己的一系列弹窗:用于提示用户的提示框.用于警 ...

  6. 原生js焦点轮播图

    原生js焦点轮播图主要注意这几点: 1.前后按钮实现切换,同时注意辅助图2.中间的button随着前后按钮对应切换,同时按button也能跳转到相应的index3.间隔调用与无限轮播.4.注意在动画时 ...

  7. iOS 图片轮播图(自动滚动)

    iOS 图片轮播图(自动滚动) #import "DDViewController.h" #define DDImageCount 5 @interface DDViewContr ...

  8. 【jQuery】百分比自适应屏幕轮播图特效

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 【JavaScript】固定布局轮播图特效

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. Centos 7.6 双网卡绑定实现高可用

    Centos 7.6 双网卡绑定实现高可用 作者:尹正杰 版权声明:原创作品, 谢绝转载!否则将追究法律责任. 一.Bond模式概述 当linux系统上有多个单独网卡,又想充分利用这些网卡,同时对外提 ...

  2. python安装脚本

    [root@dn3 hadoop]# cat install.py #!/usr/bin/python #coding=utf- import os import sys : pass else: p ...

  3. docker安装之mariadb

    https://hub.docker.com/_/mariadb?tab=description Supported tags and respective Dockerfile links 10.4 ...

  4. workerman——消息推送(web-msg-send)

    前言 说下场景,当后台将号码池的号码分配给指定客服的时候,需要给指定的客户推送一条消息告诉该客户,通讯录有新增数据. 步骤 下载 https://www.workerman.net/web-sende ...

  5. Django admin中文报错Incorrect string value 解决办法

  6. 洛谷 P2279 [HNOI2003]消防局的设立 题解

    每日一题 day34 打卡 Analysis 这道题的正解本来是树形dp,但要设5个状态,太麻烦了.于是我就用贪心试图做出此题,没想到还真做出来了. 考虑当前深度最大的叶子结点,你肯定要有一个消防局去 ...

  7. sshfs 试用

    sshfs 是基于fuse 开发的可以像使用本地系统一样,通过ssh 协议访问远端服务器文件,有好多方便的用途 数据同步 数据加密访问 做为共享数据卷(基于给容器使用) 安装 yum install ...

  8. 问题--Notepad++保存文件遇到Failed to save file

    一.问题如下 使用Notepad编码,保存时遇到问题:Failed to save file. Not enough space on disk to save file? 如下图所示: 二.解决方法 ...

  9. Codevs 1500 后缀排序(后缀数组)

    1500 后缀排序 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 天凯是MIT的新生.Prof. HandsomeG给了他一个 ...

  10. XMind 8 pro for Mac(思维导图软件)附序列号和破解教程【亲测可用!!】

    年后了,又到一年面试时,最近在用思维导图整理知识点,原本使用的是在线思维导图 ProcessOn,奈何免费版的个人文件数量只能有9 张,远远不能满足我的需要,所以还是使用一个本地版的吧,but依然不想 ...