首页 > 编程语言 >JavaScript(07): 实例2---网页广告漂浮效果(面向对象版)

JavaScript(07): 实例2---网页广告漂浮效果(面向对象版)

时间:2023-06-20 13:04:14浏览次数:37  
标签:function null 07 JavaScript getComputedStyle --- parseInt document id


在上一个版本的基础上使用JavaScript的面向对象完成,为了不影响阅读,去掉了随滚动条移动的广告

<!DOCTYPE html>

<html>
	<head>
		<title>Example</title>
		<meta http-equiv="content-type" content="text/html; charset=GBK" />
		<link type="text/css" rel="stylesheet" href="css/global.css" />
		<script type="text/javascript">
			function $(id) {
				return document.getElementById(id);
			}

			function Adv(id, speedX, speedY) {
				this.id = id;
				this.x = parseInt(document.defaultView.getComputedStyle($(id), null).left);
				this.y = parseInt(document.defaultView.getComputedStyle($(id), null).top);
				this.width = parseInt(document.defaultView.getComputedStyle($(id), null).width);
				this.height = parseInt(document.defaultView.getComputedStyle($(id), null).height);
				this.speedX = speedX;
				this.speedY = speedY;

				this.move = function() {
					var _this = this;
					setInterval(
						function() {
							var browserHeight = document.documentElement.clientHeight;
							var browserWidth = document.documentElement.clientWidth;

							if(_this.y >= browserHeight - _this.height || _this.y <= 0) {
								_this.speedY = -_this.speedY;
							}
							if(this.x >= browserWidth - this.width || this.x <= 0 ) {
								_this.speedX = -_this.speedX;
							}
								
							_this.y += _this.speedY;
							_this.x += _this.speedX;
								
							$(_this.id).style.top = _this.y + "px";
							$(_this.id).style.left = _this.x + "px";
						}, 50);
				};
			}

			window.onload = function() {
				var obj = new Adv("madv", 2, 2);
				obj.move();
			}
		</script>
	</head>

	<body style="font-family:Times New Roman">
		<h1>LUO Hao's Baidu Blog</h1>
		<hr/>
		<div id="madv">advertisement</div>
		<div class="container" align="center">
			<div class="advX"></div>
			<div class="center">
				<div style="margin:0px 0px 0px 0px"><img src="images/bg1.png"/></div>
				<div style="margin:0px 0px 0px 0px"><img src="images/bg2.png"/></div>
			</div>
			<div class="right"></div>
		</div>
	</body>
</html>

标签:function,null,07,JavaScript,getComputedStyle,---,parseInt,document,id
From: https://blog.51cto.com/u_16166070/6521807

相关文章

  • [连载]C#程序设计(08)--- C#核心编程-6 --- 方法
    ......
  • 面试编程题拾遗(05) --- 括号匹配检查
    题目:一个表达式字符串中包含了‘(’,')','[',']','{','}'六种括号,判断这些括号是否匹配。解决这个问题可以使用一种叫“栈”的数据结构,它是一种FILO(先进后出)的结构,插入(push,入栈)和删除(pop,出栈)元素都是在栈顶进行。代码如下所示:importjava.util.Stack;publicclassTest05{ publicst......
  • JavaScript(07): 实例3---Google Eye
    下面的例子源于GoogleEye(如下图所示的效果),通过这个例子可以好好体会一下JavaScript的面向对象编程。<!DOCTYPEhtml><html> <head> <title>GoogleEye</title> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <st......
  • 数据结构和算法系列课程(02) --- 线性表和贪吃蛇
    线性结构是一种具有以下特点的结构:存在唯一一个被称为“第一个”的数据元素存在唯一一个被称为“最后一个”的数据元素除第一个元素之外,集合中的每个元素均有且仅有一个前驱除最后一个元素之外,集合中的每个元素均有且仅有一个后继那么,线性表、栈、队列、数组、字符串都可以......
  • 数据结构和算法系列课程(01)--- 排序二叉树和红黑树
    把排序二叉树放在这个系列课程的第一个部分似乎有些唐突,但是考虑到它在面试中出现的可能性,把它放在这样的一个位置也就不足为奇了。关于树和二叉树的基础知识,可以到下面的链接中下载我的课件进行了解。下面给出一个排序二叉树的Java实现:packagcom.loonstudio;/***排序二叉树......
  • JavaScript的对象深度克隆
    Object.prototype.clone=function(){ varnewObj={}; for(variinthis){ if(typeof(this[i])=="object"||typeof(this[i])=="function"){ newObj[i]=this[i].clone(); } else{ newObj[i]=this[i]; } } r......
  • 一个例子帮你搞懂C#语言高级特性系列(02) --- 委托、事件和Lambda表达式
    直接看例子吧:usingSystem;usingSystem.Windows.Forms;usingSystem.Threading;namespaceCom.LoonStudio.Example{publicclassCar{//定义一个汽车事件的委托publicdelegatevoidCarEventHandler(stringmsg);//定义加速事件......
  • [连载]C#程序设计(05)--- C#核心编程-3 --- 表达式和运算符
    ......
  • [连载]C#程序设计(09)--- 类和对象
    ......
  • Java面试题集(51-70)
    Java程序员面试题集(51-70)摘要:这一部分主要讲解了异常、多线程、容器和I/O的相关面试题。首先,异常机制提供了一种在不打乱原有业务逻辑的前提下,把程序在运行时可能出现的状况处理掉的优雅的解决方案,同时也是面向对象的解决方案。而Java的线程模型是建立在共享的、默认的可见的可变状......