javascript运动效果实例总结(放大缩小、滑动淡入、滚动)
作者:lostyu 时间:2024-04-16 09:24:46
本文实例总结了javascript运动效果实现与用法。分享给大家供大家参考,具体如下:
一、图片放大缩小效果:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>图片放大缩小</title>
<style>
*{ margin:0; padding:0; list-style:none;}
#ulList{ margin:50px;}
#ulList li{ margin:10px; width:100px; height:100px; float:left; background:#ddd; border:1px solid black;}
</style>
<script>
window.onload = function()
{
var oUl = document.getElementById('ulList');
var aLi = oUl.getElementsByTagName('li');
var zIndex = 2;
//布局转换
for(var i=0;i<aLi.length;i++){
aLi[i].style.left = aLi[i].offsetLeft + 'px';
aLi[i].style.top = aLi[i].offsetTop + 'px';
}
for(var i=0;i<aLi.length;i++){
aLi[i].style.position = 'absolute';
aLi[i].style.margin = '0';
}
for(var i=0;i<aLi.length;i++){
aLi[i].onmouseover = function()
{
this.style.zIndex = zIndex++;
startMove(this, {width:200, height:200, marginLeft:-50, marginTop:-50});
};
aLi[i].onmouseout = function()
{
startMove(this, {width:100, height:100, marginLeft:0, marginTop:0});
};
}
};
function getStyle(obj, attr)
{
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj, false)[attr];
}
}
function startMove(obj, json, fn)
{
clearInterval(obj.timer);
obj.timer=setInterval(function (){
var bStop=true;
for(var attr in json)
{
var iCur=0;
if(attr=='opacity')
{
iCur=parseInt(parseFloat(getStyle(obj, attr))*100);
}
else
{
iCur=parseInt(getStyle(obj, attr));
}
var iSpeed=(json[attr]-iCur)/8;
iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
if(iCur!=json[attr])
{
bStop=false;
}
if(attr=='opacity')
{
obj.style.filter='alpha(opacity:'+(iCur+iSpeed)+')';
obj.style.opacity=(iCur+iSpeed)/100;
}
else
{
obj.style[attr]=iCur+iSpeed+'px';
}
}
if(bStop)
{
clearInterval(obj.timer);
if(fn)
{
fn();
}
}
}, 30)
}
</script>
</head>
<body>
<ul id="ulList">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
二、信息滑动淡入加载显示效果:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#msgBox{ width:500px; margin:0 auto; padding:5px;}
.msgList{ filter:alpha(opacity=0); opacity:0; font-size:12px; line-height:1.6; border-bottom:1px solid #ddd;}
.box{ float:left;}
</style>
<script>
window.onload = function()
{
var oTxt = document.getElementById('txt1');
var oBtn = document.getElementById('btn1');
var oBox = document.getElementById('msgBox');
oBtn.onclick = function()
{
var oMsg = document.createElement('div');
var aDiv = oBox.getElementsByTagName('div');
oMsg.className = 'msgList';
oMsg.innerHTML = oTxt.value;
oTxt.value = '';
if(aDiv.length==0){
oBox.appendChild(oMsg);
}else{
oBox.insertBefore(oMsg, aDiv[0]);
}
var iH = oMsg.offsetHeight;
oMsg.style.height = 0;
startMove(oMsg, {height:iH}, function(){
startMove(oMsg, {opacity:100});
});
};
};
function getStyle(obj, attr)
{
if(obj.currentStyle){
return obj.currentStyle;
}else{
return getComputedStyle(obj, false)[attr];
}
}
function startMove(obj, json, fn)
{
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var bStop = true;
for(var attr in json){
var iCur = 0;
if(attr == 'opacity'){
iCur = Math.round((parseFloat(getStyle(obj, attr))*100));
}else{
iCur = parseInt(getStyle(obj, attr));
}
var iSpeed = (json[attr] - iCur) / 8;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(iCur != json[attr]){
bStop = false;
}
if(attr == 'opacity'){
obj.style.filter = 'alpha(opacity=' + (iCur + iSpeed)+')';
obj.style.opacity = (iCur + iSpeed) / 100;
}else{
obj.style[attr] = iCur + iSpeed + 'px';
}
}
if(bStop){
clearInterval(obj.timer);
if(fn){
fn();
}
}
}, 30);
}
</script>
</head>
<body>
<div class="box">
<textarea id="txt1" cols="40" rows="10"></textarea><br />
<input id="btn1" type="button" value="提交信息" />
</div>
<div id="msgBox">
</div>
</body>
</html>
三、无缝滚动效果:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
*{ margin:0; padding:0; list-style:none;}
#div1{ width:480px; height:120px; margin:50px auto; border:1px solid black; position:relative; overflow:hidden;}
#div1 li{ float:left; padding:10px;}
#div1 li img{ display:block;}
#div1 ul{ position:absolute;}
</style>
<script>
window.onload = function()
{
var oDiv = document.getElementById('div1');
var oUl = oDiv.getElementsByTagName('ul')[0];
var aLi = oUl.getElementsByTagName('li');
var aBtn = document.getElementsByTagName('input');
var iSpeed = -3;
var timer = null;
oUl.innerHTML += oUl.innerHTML;
oUl.style.width = aLi[0].offsetWidth * aLi.length + 'px';
timer = setInterval(move, 30);
aBtn[0].onclick = function()
{
iSpeed = -3;
};
aBtn[1].onclick = function()
{
iSpeed = 3;
};
oDiv.onmouseover = function()
{
clearInterval(timer);
};
oDiv.onmouseout = function()
{
timer = setInterval(move, 30);
};
function move(){
if(oUl.offsetLeft<-oUl.offsetWidth/2){
oUl.style.left = '0px';
}else if(oUl.offsetLeft>0){
oUl.style.left = -oUl.offsetWidth/2 + 'px';
}
oUl.style.left = oUl.offsetLeft + iSpeed + 'px';
}
};
</script>
</head>
<body>
<input type="button" value="向左" />
<input type="button" value="向右" />
<div id="div1">
<ul>
<li><img src="images/1.jpg" width="100" height="100" /></li>
<li><img src="images/2.jpg" width="100" height="100" /></li>
<li><img src="images/3.jpg" width="100" height="100" /></li>
<li><img src="images/4.jpg" width="100" height="100" /></li>
</ul>
</div>
</body>
</html>
希望本文所述对大家JavaScript程序设计有所帮助。
标签:javascript,运动
0
投稿
猜你喜欢
Laravel框架表单验证格式化输出的方法
2022-05-25 04:49:52
Python中常用的8种字符串操作方法
2023-05-28 09:44:38
PHP rsa加密解密算法原理解析
2023-08-18 17:23:38
Python数据分析之使用scikit-learn构建模型
2023-11-10 23:19:10
界面设计10条可用性方面的启发
2010-04-06 17:22:00
MySQL数据库维护中监控所用到的常用命令
2024-01-28 03:23:24
ORACLE数据库查看执行计划的方法
2012-06-06 20:15:52
python网页请求urllib2模块简单封装代码
2021-07-31 02:03:55
Tensorflow加载模型实现图像分类识别流程详解
2023-12-22 02:31:13
python pyecharts库的用法大全
2021-04-21 13:48:05
关于python列表相关知识点
2021-07-25 06:54:05
python实现图片转换成素描和漫画格式
2023-09-20 14:44:25
基于python+opencv调用电脑摄像头实现实时人脸眼睛以及微笑识别
2023-07-08 23:31:14
Python实现曲线拟合的最小二乘法
2022-03-24 16:13:25
JS前端接口请求参数混淆方案分享
2024-04-18 10:53:17
如何通过pycharm实现对数据库的查询等操作(非多步操作)
2024-01-25 03:31:37
PHP文件操作详解
2023-11-20 07:44:25
MySQL通配符与正则表达式搜过滤数据详解
2024-01-23 03:12:54
python二叉树类以及其4种遍历方法实例
2023-07-25 02:22:48
Django实现全文检索的方法(支持中文)
2023-12-04 14:35:13