封装js运动函数
2016-10-11 08:32
423 查看
//js封装函数
function getstyle(obj, name) {
if(obj.currentStyle) {
return obj.currentStyle[name];
//ie浏览器获取非行间样式的值
} else {
return getComputedStyle(obj, false)[name];
//非ie浏览器获取非行间样式的值
}
}
//var timer1=null;
function move(obj, json, fn) {
//var oOne=document.getElementById('one');
clearInterval(obj.timer);
obj.timer = setInterval(function() {
var stop = true;
for(var i in json) { //i为json里面的属性名称,json[i]表示json里面属性名称对应的值;
//var at=parseInt(getstyle(obj,att));
var at = 0;
if(i == 'opacity') {
at = Math.round(parseFloat(getstyle(obj, i)) * 100);
} else {
at = parseInt(getstyle(obj, i));
}
var speed = (json[i] - at) / 10;
speed > 0 ? speed = Math.ceil(speed) : speed = Math.floor(speed);
if(at != json[i]) {
stop = false;
}
//obj.style[att]=at+speed+'px';
if(i == 'opacity') {
obj.style.opacity = (at + speed) / 100;
obj.style.filter = 'alpha(opacity:' + (at + speed) + ')';
document.getElementById('tex').value = obj.style.opacity;
} else {
obj.style[i] = at + speed + 'px';
}
}
if(stop) {
clearInterval(obj.timer);
if(fn) {
fn()
}
}
}, 30);
}
//html部分
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
padding: 0;
margin: 0;
list-style: none;
text-decoration: none;
}
ol{
position: absolute;
right: 170px;
bottom: 7px;
z-index: 20;
}
ol li {
width: 20px;
height: 20px;
border: 1px solid #c47d3f;
text-align: center;
float: left;
margin-left: 3px;
background-color: #f7f1cf;
font-size: 9px;
font-weight: bold;
line-height: 20px;
border-radius: 20px;
}
ol li a {
color: #ac6935;
}
.box{width: 470px;height: 150px;margin: 150px 0 0 400px;background-color: yellow;position: relative;overflow: hidden;}
.active a{color: #fff;}
.active{background-color: #efbc3f;}
#ul_img{position: absolute;top: 0;left: 0;z-index: 10;width: 2350px;height: 150px;}
#ul_img li{float: left;}
#ul_img img{display: block;}
</style>
<script type="text/javascript" src="../js/js.js"></script>
<script type="text/javascript">
window.onload=function(){
var oNumber=document.getElementById('number');
var aLi1=oNumber.getElementsByTagName('li');
var oUl_img=document.getElementById('ul_img');
var aLi2=oUl_img.getElementsByTagName('li');
var oBox=document.getElementById('box');
var now=0;
var timer=null;
for(var i=0;i<aLi1.length;i++){
aLi1[i].index=i;//给每个li添加一个下标
aLi1[i].onclick=function(){
now=this.index;//鼠标点击的时候,将点击的那个li的下标赋值给now
tab();
};
}
function tab(){
for(var i=0;i<aLi1.length;i++){
aLi1[i].className='';
}
aLi1[now].className='active';//aLi1[now]等于aLi1[this.index],但是此时ali已经不是在onclick事件里面,所以不能运用this,声明now变量的作用就在这里
move(oUl_img,{left:-470*now})
}
function next(){
now++;
if(now==aLi2.length){
now=0;
}
tab();
}
timer=setInterval(next,3000);
};
</script>
</head>
<body>
<div class="box" id="box">
<ol id="number">
<li class="active">
<a href="#">1</a>
</li>
<li>
<a href="#">2</a>
</li>
<li>
<a href="#">3</a>
</li>
<li>
<a href="#">4</a>
</li>
<li>
<a href="#">5</a>
</li>
</ol>
<ul id="ul_img">
<li>
<a href="#"><img src="../img/10.jpg" /></a>
</li>
<li>
<a href="#"><img src="../img/13.jpg" /></a>
</li>
<li>
<a href="#"><img src="../img/14.jpg" /></a>
</li>
<li>
<a href="#"><img src="../img/15.jpg" /></a>
</li>
<li>
<a href="#"><img src="../img/16.jpg" /></a>
</li>
</ul>
</div>
</body>
</html>
function getstyle(obj, name) {
if(obj.currentStyle) {
return obj.currentStyle[name];
//ie浏览器获取非行间样式的值
} else {
return getComputedStyle(obj, false)[name];
//非ie浏览器获取非行间样式的值
}
}
//var timer1=null;
function move(obj, json, fn) {
//var oOne=document.getElementById('one');
clearInterval(obj.timer);
obj.timer = setInterval(function() {
var stop = true;
for(var i in json) { //i为json里面的属性名称,json[i]表示json里面属性名称对应的值;
//var at=parseInt(getstyle(obj,att));
var at = 0;
if(i == 'opacity') {
at = Math.round(parseFloat(getstyle(obj, i)) * 100);
} else {
at = parseInt(getstyle(obj, i));
}
var speed = (json[i] - at) / 10;
speed > 0 ? speed = Math.ceil(speed) : speed = Math.floor(speed);
if(at != json[i]) {
stop = false;
}
//obj.style[att]=at+speed+'px';
if(i == 'opacity') {
obj.style.opacity = (at + speed) / 100;
obj.style.filter = 'alpha(opacity:' + (at + speed) + ')';
document.getElementById('tex').value = obj.style.opacity;
} else {
obj.style[i] = at + speed + 'px';
}
}
if(stop) {
clearInterval(obj.timer);
if(fn) {
fn()
}
}
}, 30);
}
//html部分
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
padding: 0;
margin: 0;
list-style: none;
text-decoration: none;
}
ol{
position: absolute;
right: 170px;
bottom: 7px;
z-index: 20;
}
ol li {
width: 20px;
height: 20px;
border: 1px solid #c47d3f;
text-align: center;
float: left;
margin-left: 3px;
background-color: #f7f1cf;
font-size: 9px;
font-weight: bold;
line-height: 20px;
border-radius: 20px;
}
ol li a {
color: #ac6935;
}
.box{width: 470px;height: 150px;margin: 150px 0 0 400px;background-color: yellow;position: relative;overflow: hidden;}
.active a{color: #fff;}
.active{background-color: #efbc3f;}
#ul_img{position: absolute;top: 0;left: 0;z-index: 10;width: 2350px;height: 150px;}
#ul_img li{float: left;}
#ul_img img{display: block;}
</style>
<script type="text/javascript" src="../js/js.js"></script>
<script type="text/javascript">
window.onload=function(){
var oNumber=document.getElementById('number');
var aLi1=oNumber.getElementsByTagName('li');
var oUl_img=document.getElementById('ul_img');
var aLi2=oUl_img.getElementsByTagName('li');
var oBox=document.getElementById('box');
var now=0;
var timer=null;
for(var i=0;i<aLi1.length;i++){
aLi1[i].index=i;//给每个li添加一个下标
aLi1[i].onclick=function(){
now=this.index;//鼠标点击的时候,将点击的那个li的下标赋值给now
tab();
};
}
function tab(){
for(var i=0;i<aLi1.length;i++){
aLi1[i].className='';
}
aLi1[now].className='active';//aLi1[now]等于aLi1[this.index],但是此时ali已经不是在onclick事件里面,所以不能运用this,声明now变量的作用就在这里
move(oUl_img,{left:-470*now})
}
function next(){
now++;
if(now==aLi2.length){
now=0;
}
tab();
}
timer=setInterval(next,3000);
};
</script>
</head>
<body>
<div class="box" id="box">
<ol id="number">
<li class="active">
<a href="#">1</a>
</li>
<li>
<a href="#">2</a>
</li>
<li>
<a href="#">3</a>
</li>
<li>
<a href="#">4</a>
</li>
<li>
<a href="#">5</a>
</li>
</ol>
<ul id="ul_img">
<li>
<a href="#"><img src="../img/10.jpg" /></a>
</li>
<li>
<a href="#"><img src="../img/13.jpg" /></a>
</li>
<li>
<a href="#"><img src="../img/14.jpg" /></a>
</li>
<li>
<a href="#"><img src="../img/15.jpg" /></a>
</li>
<li>
<a href="#"><img src="../img/16.jpg" /></a>
</li>
</ul>
</div>
</body>
</html>
相关文章推荐
- js封装多物体运动函数
- JS实现运动缓冲效果的封装函数示例
- js中封装多物体运动及透明度变化函数
- 原生JS 封装运动函数
- 原生js封装元素运动函数
- 【js事件详解】js事件封装函数,js跨浏览器事件处理机制
- 【js事件详解】js事件封装函数,js跨浏览器事件处理机制
- JS封装函数打造横向滑动的图片切换效果
- [妙味JS基础]第九课:定时器管理、函数封装
- 封装getByClass(JS获取class的方法封装为一个函数)
- JS封装的几个位操作函数和16进制操作函数
- js 函数简单封装
- JS手风琴特效、运动函数
- JS封装cookie操作函数实例(设置、读取、删除)
- 【js事件详解】js事件封装函数,js跨浏览器事件处理机制
- js 封装的函数 总结
- js笔记---封装一般运动
- js 封装cookie操作的函数代码
- js 中有关运动的函数
- 简单的JS运动封装实例---侧栏分享到