纯js写图片轮播插件
最近终于写成了自己创作的图片轮播插件,使用原生js编写。与目前网上流行的轮播插件相比,功能和效果稍弱,但是使用起来相当方便。
先看html代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>图片轮播</title> <link rel="stylesheet" type="text/css" href="pmmpig.css">
</head>
<body>
<div style="width: 600px; height: 300px; margin: 0 auto;">
<div class="pigwrap">
<ul>
<li><img src="imgs/1.jpg"><a href="#">凤凰岭</a></li>
<li><img src="imgs/2.jpg"><a href="#">凤凰岭梨花</a></li>
<li><img src="imgs/3.jpg"><a href="#">外滩</a></li>
<li><img src="imgs/4.jpg"><a href="#">威海日出</a></li>
<li><img src="imgs/5.jpg"><a href="#">北京站</a></li>
<li><img src="imgs/6.jpg"><a href="#">泰山</a></li>
</ul>
</div>
</div>
<script type="text/javascript" src="pmmpig.js"></script>
</body>
</html>
下面是CSS样式修饰部分
.pigwrap{
width: 550px;
height: 300px;
position: relative;
margin:;
padding:;
overflow: hidden;
}
.pigwrap ul{
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
}
.pigwrap li{
float: left;
list-style: none;
height: 100%;
}
.pigwrap li img{
width: 100%;
height: 100%;
cursor: pointer;
}
.pigwrap li a {
display: block;
width: 100%;
height: 40px;
margin-top: -40px;
font-weight: bold;
padding-left: 10px;
color: #fff;
font-size: 16px;
line-height: 40px;
font-family: "微软雅黑"
}
.pigwrap li a:link, .pigwrap li a:visited{
text-decoration: none;
}
.pigwrap li a:hover, .pigwrap li a:active{
text-decoration: underline;
}
.foot{
width: 100%;
height: 20px;
position: absolute;
bottom: 40px;
text-align: center;
}
.circle{
background-color: #f5f5f5;
width: 15px;
height: 15px;
margin: 0 auto;
display: inline-block;
border-radius: 7px;
margin-right: 10px;
cursor: pointer;
display: none;
opacity: 0.3;
}
.gobtn{
display: none;
position: absolute;
top: 100px;
left: 0px;
width: 100%;
height: 60px;
overflow: hidden;
}
.gobtn .btnleft{
float: left;
margin-left: 10px;
background-image: url("../imgs/btn.gif");
cursor: pointer;
}
.gobtn .btnright{
float: right;
margin-right: 10px;
margin-top: -60px;
background-image: url("../imgs/btn.gif");
cursor: pointer;
}
下面是js部分
var pigNum = 0;
var current = 1;
var speed = 10;
var timer = null;
var contro = null;
var stop = 3000;
var owrap = null;
var oul = null;
var pigWidth = 0; (function () {
// body...
init();
nextPig();
contro = setInterval(function(){
nextPig();
}, stop);
})();
function init() {
owrap = document.getElementsByClassName("pigwrap")[0];
oul = owrap.getElementsByTagName("ul")[0];
pigNum = oul.getElementsByTagName("li").length;
pigWidth = owrap.clientWidth; initView(); owrap.onmouseover = function (e) {
// body...
mouseoverHandler();
}
owrap.onmouseout = function () {
// body...
mouseoutHandler();
}
}
function initView () {
// body...
var lis = oul.getElementsByTagName("li");
for (var i = 0; i < lis.length; ++i){
var li = lis[i];
li.style.width = pigWidth + "px";
}
oul.style.width = pigWidth*pigNum + "px"; var footdiv = document.createElement("div");
footdiv.setAttribute("class", "foot");
var cirs = "";
for(var i = 0; i < pigNum; ++i){
cirs += "<div class='circle' onclick='topig("+(i+1)+")'></div>";
}
footdiv.innerHTML = cirs;
owrap.appendChild(footdiv); var btndiv = document.createElement("div");
btndiv.setAttribute("class", "gobtn");
btndiv.innerHTML =
"<div><img src='imgs/btn.gif' class='btnleft' onclick='toLastPig()'></div>"+
"<div><img src='imgs/btn.gif' class='btnright' onclick='toNextPig()'></div>";
owrap.appendChild(btndiv);
}
function mouseoutHandler () {
// body...
var cirs = owrap.getElementsByClassName("circle");
for(var i = 0; i < cirs.length; ++i){
cirs[i].style.display = "none";
}
cirs[current-1].style.opacity = 0.3; var btns = document.getElementsByClassName("gobtn");
btns[0].style.display = "none"; clearInterval(contro);
contro = setInterval(function(){
nextPig();
}, stop);
}
function mouseoverHandler () {
// body...
var cirs = owrap.getElementsByClassName("circle");
for(var i = 0; i < cirs.length; ++i){
cirs[i].style.display = "inline-block";
}
cirs[current-1].style.opacity = 0.9; var btns = document.getElementsByClassName("gobtn");
btns[0].style.display = "block"; clearInterval(contro);
}
function toLastPig () {
// body...
if (current == 1)
topig(pigNum);
else
topig(current-1);
}
function toNextPig () {
// body...
if (current == pigNum)
topig(1);
else
topig(current+1);
}
function topig (index) {
// body...
clearInterval(contro); //纠正位置
oul.style.left = -pigWidth*(current-1) + "px"; var cirs = owrap.getElementsByClassName("circle");
cirs[current-1].style.opacity = 0.3; startMove(oul, pigWidth*(index-current), speed);
current = index;
contro = setInterval(function(){
nextPig();
}, stop); cirs[current-1].style.opacity = 0.9;
} function nextPig (argument) {
// body...
var ow = owrap;
var ul = oul; //纠正位置
ul.style.left = -pigWidth*(current-1) + "px"; current++;
if (current > pigNum) {
current = 1;
startMove(ul, -pigWidth*(pigNum-1), speed);
return ;
}
startMove(ul, pigWidth, speed);
} function startMove (elemet, length, x) {
// body...
clearInterval(timer);
var speed = 0;
timer = setInterval(function(){
if(Math.abs(length) <= Math.abs(speed)){
clearInterval(timer);
elemet.style.left = elemet.offsetLeft - length + "px";
}else{
speed = length / x;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
length -= speed;
elemet.style.left = elemet.offsetLeft - speed + "px";
}
}, 30);
}
纯js写图片轮播插件的更多相关文章
- PgwSlideshow-基于Jquery的图片轮播插件
0 PgwSlideshow简介 PgwSlideshow是一款基于Jquery的图片轮播插件,基本布局分为上下结构,上方为大图轮播区域,用户可自定义图片轮播切换的间隔时间,也可以通过单击左右方向按键 ...
- 结构-行为-样式-requireJs实现图片轮播插件
最近工作需要,就自己写了一个图片轮播插件,不过想到要集成到框架中,于是又用RequireJs改了一遍. 主要文件: style.css jquery-1.11.1.min.js require.js ...
- 图片轮播插件-carouFredSel
carouFredSel图片轮播插件基于Jquery,比较常规的轮播插件,支持滚轮及键盘左右按键,加入其它插件可实现更加复杂的特效. 主页地址:http://caroufredsel.dev7stud ...
- JQuery插件之图片轮播插件–slideBox
来源:http://www.ido321.com/852.html 今天偶然发现了一个比较好用的图片轮播插件—slideBox 先看看效果:http://slidebox.sinaapp.com/ 代 ...
- Javascript和jQuery WordPress 图片轮播插件, 内容滚动插件,前后切换幻灯片形式显示
用于在有限的网页空间内展示一组产品图片或者照片,同时还有非常吸引人的动画效果.本文向大家推荐12款实用的 jQuery 图片轮播效果插件,帮助你在你的项目中加入一些效果精美的图片轮播效果,希望这些插件 ...
- 图片轮播插件Nivo Slider
推荐:图片轮播插件Nivo Slider 因为项目需要一款切换样式多一些的轮播插件,不经意找到了NivoSlider,非常好用,比bootstrap要好用,而且样式丰富.值得注意的是, ...
- 面板支持单个,多个元素的jQuery图片轮播插件
一.先附上demo <!doctype html> <html> <head> <meta charset="utf-8"> < ...
- 原生js实现图片轮播思路分析
一.复习原生js实现图片轮播 1.要点 自动轮播 点击小圆圈按钮,显示相应图片 点击左右箭头,实现向前向后轮播图片 2.实现思路 <div id="container"> ...
- 12款经典的白富美型—jquery图片轮播插件—前端开发必备
图片轮播是网站中的常用功能,用于在有限的网页空间内展示一组产品图片或者照片,同时还有非常吸引人的动画效果.本文向大家推荐12款实用的 jQuery 图片轮播效果插件,帮助你在你的项目中加入一些效果精美 ...
随机推荐
- Mac 上Dock中添加“最近打开过的项目”(Recent Applications)
有一个特别有用的Stack,“最近打开过的项目”,建立方法如下: 1. 打开Terminal,输入以下命令 defaults write com.apple.dock persistent-other ...
- 【转】NI语法 JNI参考 JNI函数大全
原文网址:http://blog.sina.com.cn/s/blog_5de73d0b0101chk1.html 一.对照表 Java类型 本地类型 描述boolean ...
- 在Sharepoint2010中发布VS2010开发的WebPart
转:http://www.cnblogs.com/bfgl/archive/2012/03/22/2411698.html 本人接触Sharepoint2010到今天为止不到一个月.作为一名老C#程序 ...
- FZU 2237 中位数 主席树 树上k大
#include <cstdio> #include <cstring> #include <queue> #include <set> #includ ...
- getchar
getchar函数每次只从缓冲区中接受一个字符. getchar有一个int型的返回值. 当程序调用getchar时,程序就等着用户按键,用户输入的字符被存放在键盘缓冲区中,直到用户按回车为止(回车字 ...
- HW3.23
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- hdoj 1898 Sempr == The Best Problem Solver?
Sempr == The Best Problem Solver? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/3276 ...
- 检查REDO日志相关信息并生成HTML文件的脚本
生成HTML格式的文件 内容有: 检查数据库版本.REDO日志组情况, 最近20次日志切换频率检查--日志间的归档时间间隔, 这对查看数据库的IO繁忙时段 统计指定日期当天每小时的归档日志产生量--日 ...
- cmd 里面运行git提示“不是内部或外部命令,也不是可运行的程序”的解决办法
1.找到你电脑上的git安装中bin的路径,如:E:\安装吧\Git\Git\bin:同时,找到git安装路径中git-core的位置,如:E:\安装吧\Git\Git\libexec\git-cor ...
- html图片滚动效果
分享一个手动控制图片左右滚动的代码 先说html部分,建立一个层,写出他的样式,层中在建立一个小一点的层用来存放需要滚动的图片,小层两边再建两个小层用来存放控制图片左右滚动的按钮.代码如下: 样式表: ...