首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • JavaScript 弹簧效果 [已结贴,结贴人:cloudgamer]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • cloudgamer
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    • 揭帖率:
    发表于:2008-05-17 14:58:44 楼主
    做了个类似弹簧的效果
    欢迎指点
    效果看这里

    代码
    HTML code
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>弹簧效果</title> <script type="text/javascript"> var $ = function (id) { return "string" == typeof id ? document.getElementById(id) : id; }; function addEventHandler(oTarget, sEventType, fnHandler) { if (oTarget.addEventListener) { oTarget.addEventListener(sEventType, fnHandler, false); } else if (oTarget.attachEvent) { oTarget.attachEvent("on" + sEventType, fnHandler); } else { oTarget["on" + sEventType] = fnHandler; } }; var Class = { create: function() { return function() { this.initialize.apply(this, arguments); } } } Object.extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } return destination; } var Bounce = Class.create(); Bounce.prototype = { initialize: function(obj, iRange, options) { this.obj = $(obj); this._X = this.X = parseInt(this.obj.style.left) || 0; this._side = -1; this._steps = []; this.Max = this.Range = iRange || 0; this.SetOptions(options); this.Step = Math.abs(this.options.Step); this.Time = Math.abs(this.options.Time); this.Zoom = Math.abs(this.options.Zoom); this.Reduce = !!this.options.Reduce; this.Min = Math.abs(this.options.Min); this.Max = Math.abs(this.options.Max); this.onMin = this.options.onMin; this.onMax = this.options.onMax; this.Start(this.Range); }, //设置默认属性 SetOptions: function(options) { this.options = {//默认值 Step: 10,//滑动变化率 Time: 10,//滑动延时 Zoom: 0,//缩放变化率 Reduce: true,//是否缩小 Min: 0,//最小范围 Max: 0,//最大范围 onMin: function(){},//到达最小时函数 onMax: function(){}//到达最大时函数 }; Object.extend(this.options, options || {}); }, // Start: function(iRange) { if(this.Reduce && (iRange <= 0 || iRange <= this.Min)) { this.onMin();return; } if(!this.Reduce && (this.Max > 0 && iRange >= this.Max)) { this.onMax();return; } this.Range = iRange; this.obj.style.left = this._X + "px"; this._xs = [this._X + iRange, this._X, this._X - iRange, this._X]; this._side = -1; this.Set(); }, // Set: function() { if(this._xs.length <= 0){ this.Start(this.Zoom > 0 ? (this.Reduce ? -1 : 1) * this.Zoom + this.Range : this.Range); return; } this.X = this._xs.shift(); this._steps = GetStep(this.X, parseInt(this.obj.style.left), this.Step) if(this._side > 0) this._steps.reverse(); this._side *= -1; this.Move(); }, // Move: function() { clearTimeout(this._timer); if(this._steps.length <= 0) {this._steps = [0];} var iNow = parseInt(this.obj.style.left), iStep = this._steps.shift(); if (iStep == 0) { this.Set(); } else { this.obj.style.left = (iNow + iStep) + "px"; var oThis = this; this._timer = setTimeout(function(){ oThis.Move(); }, this.Time); } } }; //从大到小 function GetStep(iTarget, iNow, iStep){ var arrStep=[]; iTarget=parseInt(iTarget); iNow=parseInt(iNow); iStep=parseInt(iStep); (function _GetStep(iNow){ var i = (iTarget - iNow) / iStep; if (i == 0) { return; } else if (Math.abs(i) < 1) { i = i > 0 ? 1 : -1; } i=parseInt(i) arrStep[arrStep.length]=i; _GetStep(iNow+i); })(iNow) return arrStep; } window.onload=function(){ var o = new Bounce("idBounce", 200, { Zoom: 20, Max: 200, onMax: function(){o.Reduce=true;o.Start(200);}, onMin: function(){o.Reduce=false;o.Start(0);} }); new Bounce("idBounce1", 200); var o2 = new Bounce("idBounce10"); $("bb").onclick = function(){ o2.Start(parseInt($("aa").value) || 200); } } </script> </head> <body> 固定范围反弹: <div style="position:relative; border:1px solid #000000;height:50px; width:500px;"> <div id="idBounce1" style="width:10px; height:10px; background:#000000; position:absolute; z-index:99; top:20px; left:250px;"> </div> </div> <br /> 范围渐变反弹: <div style="position:relative; border:1px solid #000000; height:50px; width:500px;"> <div id="idBounce" style="width:10px; height:10px; background:#000000; position:absolute; z-index:99; top:20px; left:250px;"> </div> </div> <br /> 自定范围反弹: <div style="position:relative; border:1px solid #000000;height:50px; width:500px;"> <div id="idBounce10" style="width:10px; height:10px; background:#000000; position:absolute; z-index:99; top:20px; left:250px;"> </div> </div> <br /> 范围: <input id="aa" name="" type="text" value="200" size="8" /> <input id="bb" name="" type="button" value=" 开始 " /> <br /> </body> </html>
    20  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • mingxuan3000
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-17 15:00:271楼 得分:6
    jf
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • s_liangchao1s
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    • 3

    发表于:2008-05-17 15:04:092楼 得分:7
    学习了 不错啊
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • lawrendc
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-17 16:04:373楼 得分:7
    学习了
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved