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="data:images/1.jpg" width="100" height="100" /></li>
- <li><img src="data:images/2.jpg" width="100" height="100" /></li>
- <li><img src="data:images/3.jpg" width="100" height="100" /></li>
- <li><img src="data:images/4.jpg" width="100" height="100" /></li>
- </ul>
- </div>
- </body>
- </html>
javascript每日一练(十三)——运动实例的更多相关文章
- javascript每日一练—运动
1.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...
- javascript每日一练(十四)——弹性运动
一.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...
- javascript每日一练(十二)——运动框架
运动框架 可以实现多物体任意值运动 例子: <!doctype html> <html> <head> <meta charset="utf-8&q ...
- javascript每日一练(十一)——多物体运动
一.多物体运动 需要注意:每个运动物体的定时器作为物体的属性独立出来互不影响,属性与运动对象绑定,不能公用: 例子1: <!doctype html> <html> <h ...
- javascript每日一练(十)——运动二:缓冲运动
一.缓冲运动 实现原理:(目标距离-当前距离) / 基数 = 速度(运动距离越大速度越小,运动距离和速度成反比) (500 - oDiv.offsetLeft) / 7 = iSpeed; 需要注意: ...
- javascript每日一练(九)——运动一:匀速运动
一.js的运动 匀速运动 清除定时器 开启定时器 运动是否完成:a.运动完成,清除定时器:b.运动未完成继续 匀速运动停止条件:距离足够近 Math.abs(当然距离-目标距离) < 最小运动 ...
- javascript每日一练(二)——javascript(函数,数组)
一.函数 什么是函数 function show(){}//不带参数 function show(){return 123;}//不带参数,有返回值 function show(arg1, arg2, ...
- javascript每日一练(一)——javascript基础
一.javascript的组成 ECMAScript DOM BOM 二.变量类型 常见类型有:number, string, boolean, undefined, object, function ...
- javascript每日一练(八)——事件三:默认行为
一.阻止默认行为 return false; 自定义右键菜单 <!doctype html> <html> <head> <meta charset=&quo ...
随机推荐
- Android JNI入门第二篇——Java参数类型与本地参数类型对照
前面一篇通过简单的例子介绍了android中JNI的使用.这一篇从基础上了解一些Java参数类型与本地参数类型区别. 1) java中的返回值void和JNI中的void是完全对应的哦! ...
- MVC3.0部署问题小结
环境:MVC3.0,IIS7 Mvc3.0的部署除像正常部署aspx程序一样外,另外还需要注意的几点:1. 安装MVC3.0 确保服务器上安装了MVC3.0,默认版本是“3.0.20105.0” 2. ...
- 快速构建ASP.NET MVC Admin主页
前言 后台开发人员一般不喜欢调样式,搞半天样式出不来,还要考虑各种浏览器兼容,费心费力不讨好,还好互联网时代有大量的资源共享,避免我们从零开始,现在就来看怎么快速构建一个ASP.NET MVC后台管理 ...
- C++学习之虚继承
http://blog.csdn.net/wangxingbao4227/article/details/6772579 C++中虚拟继承的概念 为了解决从不同途径继承来的同名的数据成员在内存中有不同 ...
- Python之美[从菜鸟到高手]--urlparse源码分析
urlparse是用来解析url格式的,url格式如下:protocol :// hostname[:port] / path / [;parameters][?query]#fragment,其中; ...
- C#学习日记之数据库连接
一.webconfig设置和参数解释 在C#中新建一个网站时,webconfig文件中会有一个默认的数据库连接语句,如下 <connectionStrings> <add name= ...
- Oracle 字段是多个值的字符串的查询处理
1.创建两张表一张用户表(T_User),一张兴趣小组表T_Group,其中小组成员字段存储用户ID列表以逗号隔开, 表:T_User 编号(F_ID) 名称(用户名) 1 张三 2 李四 3 王五 ...
- myeclipse部署时An internal error occurred 错误的几种情况
myecplise上将工程部署到应用下时,经常出现 An internal error occurred during: "Add Deployment". java.lang.N ...
- 本地机apache配置基于域名的虚拟主机详解
1.打开apache的httpd.conf文件,找到# Virtual hosts#Include conf/extra/httpd-vhosts.conf这一段把Include conf/extra ...
- PHP中magic_quotes_gpc和 magic_quotes_runtime区别及其反斜线转义问题
php中关于反斜线转义: php中数据的魔法引用函数 magic_quotes_gpc 或 magic_quotes_runtime 设置为on时,当数据遇到 单引号' 和 双引号&quo ...