JS高级之运动篇
来源:互联网 发布:mysql用户密码修改 编辑:程序博客网 时间:2024/06/10 19:20
var oDiv=document.getElementById('div1');
setInterval(function(){
oDiv.style.left=oDiv.offsetLeft+10+'px';
},30);
}
offset代表当前对象的当前left值。
2.要想停止setInterval,之前必须为interval设置一个变量。clearInterval(timer);
3.if...else..对立条件下分别做不同的事。
4.startMove函数:<script type="text/javascript">
var timer=null;
function startMove(){
var oDiv=document.getElementById('div1');
clearInterval(timer);
timer=setInterval(function(){
var iSpeed=9;
if(oDiv.offsetLeft>=300){
clearInterval(timer);//到达终点时要做的事
}else {
oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px';//到达终点之前要做的事
}
},30);
}
</script>
5.position:absolute;如果父级有定位属性,则是相对父级进行定位。
6.span是为行级元素设置样式的。
7.分享到鼠标移入移出行为:
<style>#div1{width:100px;height:200px;background: #ccc;position:absolute;left:-100px;}
#div1 span{width:20px;height:60px;line-height: 20px;text-align: center;
position:absolute;left:100px;top:70px;background: yellow;}
</style>
<script>
window.onload=function(){
var oDiv=document.getElementById('div1');
oDiv.onmouseover=function(){
startMove();
}
oDiv.onmouseout=function(){
startMove2();
}
};
var timer=null;
function startMove(){
var oDiv=document.getElementById('div1');
clearInterval(timer);
timer=setInterval(function(){
if(oDiv.offsetLeft==0){
clearInterval(timer);
}else {
oDiv.style.left = oDiv.offsetLeft + 10 + 'px';
}
}
,30);
}
function startMove2(){
var oDiv=document.getElementById('div1');
clearInterval(timer);
timer=setInterval(function(){
iSpeed=-10;
if(oDiv.offsetLeft==-100){
clearInterval(timer);
}else {
oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px';
}
}
,30);
}
</script>
</head>
<body>
<div id="div1">
<span>分享到</span>
</div>
</html>
8.如何合并2个长得差不多的函数?很简单,将不同的部分作为参数传入函数。
<script>
window.onload=function(){
var oDiv=document.getElementById('div1');
oDiv.onmouseover=function(){
startMove(10,0);
}
oDiv.onmouseout=function(){
startMove(-10,-100);
}
};
var timer=null;
function startMove(iSpeed,iTaget){
var oDiv=document.getElementById('div1');
clearInterval(timer);
timer=setInterval(function(){
if(oDiv.offsetLeft==iTaget){
clearInterval(timer);
}else {
oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px';
}
}
,30);
}
</script>
9.符合生活习惯,对外接口参数越少越好(在通过方法可以解决的前提下,通过目标点计算出速度)
var iSpeed=null;
if(oDiv.offsetLeft<iTaget){
iSpeed=10;
}else{
iSpeed=-10;
}
10、图片路径:../../1.jpg(与其上级的上级同级) 1.jpg是与所在html同级的意思。
11、用变量存储透明度
<style>
#img1{
filter:alpha(opacity:30);opacity:0.3;
}
</style>
<script type="text/javascript">
window.onload=function(){
var oImg=document.getElementById('img1');
oImg.onmouseover=function(){
startMove(100);
};
oImg.onmouseout=function(){
startMove(30);
}
};
var timer=null;
var alpha=30;
function startMove(iTaget){
var oImg=document.getElementById('img1');
clearInterval(timer);
timer=setInterval(function(){
if(alpha<iTaget){
iSpeed=1;
}else{
iSpeed=-1;
}
if(alpha==iTaget){
clearInterval(timer)
}else{
alpha+=iSpeed;
oImg.style.filter='alpha(opacity:'+alpha+')';
oImg.style.opacity=alpha/100;
document.title=alpha;
}
},30);
}
</script>
</head>
<body>
<img id="img1" src="../../csdn.jpg"/>
</body>
</html>
- JS高级之运动篇
- JS高级之缓冲运动篇
- JS之运动框架
- (39)JS运动之缓冲运动
- [js运动动画]运动之匀速运动框架
- (36)JS运动之使物体向右运动
- (43)JS运动之链式运动框架
- 《js动画效果》之同时运动兼完美运动框架
- 《js动画效果》之同时运动兼完美运动框架
- 运动框架改进之完美运动框架 -纯js
- js运动-链式运动
- js运动-同时运动
- js运动-弹性运动
- js运动-碰撞运动
- js运动-运动缓冲
- js特效之文本方形运动
- (38)JS运动之淡入淡出
- 《js动画效果》之链式运动
- jQuery-DataTabls使用
- Git 本地仓库,消除分支之间的影响
- JavaIO加强(1)
- 在django中,python3.x连接数据库需要注意的地方
- java 入门学习笔记(封装,继承,多态)
- JS高级之运动篇
- google vr sdk(全景图的播放)
- Spring@Autowired注解与自动装配
- Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'
- spring中关于<context:component-scan>的使用说明
- Ehcache与redis比较
- Java HashMap的死循环
- Java序列化与反序列化
- 大数据系列修炼-Scala课程09