重力效果v1.0

本文介绍了一种使用JavaScript创建简单网页重力效果的方法。通过监听点击事件触发苹果下落动画,并利用速度和位置更新来模拟重力作用。文章包含了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 主要代码:

ExpandedBlockStart.gif View Code
 1  function gravity() {
 2      this.init();
 3      this.speedY = 0;
 4      this.clientY =  this.getViewport().height;
 5 }
 6 gravity.prototype = {
 7     init:  function () {
 8          var newDiv = document.createElement("div");
 9         newDiv.id = "box";
10          var newImg = document.createElement("img");
11         newImg.src = "apple.png";
12         newDiv.appendChild(newImg);
13         document.body.appendChild(newDiv);
14 
15          var _this =  this;
16          this.addHandler(newDiv, "click",  function () {
17             setInterval( function () { _this.shake(newDiv) }, 30);
18         });
19     },
20 
21     shake:  function (oBox) {
22          this.speedY += 2;
23          var t = oBox.offsetTop +  this.speedY;
24          if (t >  this.clientY - oBox.offsetHeight) {
25             t =  this.clientY - oBox.offsetHeight;
26              this.speedY *= -1;
27              this.speedY *= 0.75;
28              this.speedY = Math.round( this.speedY);
29         }
30          else  if (t < 0) {
31             t = 0;
32              this.speedY *= -1;
33              this.speedY *= 0.75;
34              this.speedY = Math.round( this.speedY);
35         }
36         oBox.style.top = t + 'px'
37     },
38     getViewport:  function () {
39          if (document.compatMode == "BackCompat") {
40              return {
41                 width: document.body.clientWidth,
42                 height: document.body.clientHeight
43             };
44         }
45          else {
46              return {
47                 width: document.documentElement.clientWidth,
48                 height: document.documentElement.clientWidth
49             };
50         }
51     },
52     addHandler:  function (element, type, handler) {
53          if (element.addEventListener) {
54             element.addEventListener(type, handler,  false);
55         }
56          else  if (element.attachEvent) {
57             element.attachEvent("on" + type, handler);
58         }
59          else {
60             element["on" + type] = handler;
61         }
62     },
63 
64     removeHandler:  function (element, type, handler) {
65          if (element.removeEventListener) {
66             element.removeEventListener(type, handler,  false);
67         }
68          else  if (element.detachEvent) {
69             element.detachEvent("on" + type, handler);
70         }
71          else {
72             element["on" + type] =  null;
73         }
74     }
75 }

 

/Files/MasterHeng/重力效果.zip

转载于:https://www.cnblogs.com/MasterHeng/archive/2011/11/28/2266106.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值