JavaScript 快速入门之第五章 JS与CSS交互

一:css样式的类型:

  • 行内样式:结构的内部,即写在标签内的样式;写在标签的开始部分内部,style属性当中。

  • 例:<标记 style="样式的属性名1:样式的属性值1;属性名2:属性值2;......"></标记>

  • 内部样式:写在HTML页面内部,存放于head标记当中,样式写在style标记内。

  • 例:<style>选择器 {属性名:属性值;属性名:属性值;......}</style>

  • 外部样式:写在css文件内

二:js获取css样式的方式:

  • 行内样式获取法 

  • 通过element.style.attr(元素.style.属性)即可获取可设置

  • style属性在获取值时,只能获取行内样式的值

  • 非行内样式获取法

  • 因浏览器的不同又分为两种,即基于IE浏览器的 和 非IE浏览器的如谷歌火狐等。

  1. 基于IE浏览器的非行内获取法:通过 element.currentStyle['attr']

  2.  基于非IE如火狐谷歌等非行内获取法:通过 getComputedStyle(element.null/伪类)[attr]

    注意:非行内样式获取法,只能获取不能设置。

三:浏览器的兼容性问题解决方法:

  • 设置一个函数,封装方法,兼容所有的浏览器,方便多次调用

​
    function getStyle(obj, name) { //obj:操作哪一个标签 name:该标签的属性
        if (obj.currentStyle) { //兼容IE
            return obj.currentStyle[name];
        } else { //兼容非IE浏览器---谷歌  火狐等等
            return getComputedStyle(obj, false)[name];
        }
    }

​
  • 案例:鼠标移入移除改变图片的边框颜色

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		img{
			border:5px solid yellow;
			/*display: block;*/
		}

		.over{
			border:5px solid red;
		}
		.out{
			border:5px solid yellow;
		}


	</style>

	<script type="text/javascript">
		window.onload = function(){
			var oImgs = document.getElementsByTagName("img");
			for(var i = 0;i<oImgs.length;i++){
				oImgs[i].onmouseover = function(){				
					this.className = "over";
				};
				oImgs[i].onmouseout = function(){
					this.className = "out";
				};
			}
		};

	</script>
</head>
<body>
	<!-- 
		需求:鼠标移入img 显示边框颜色红色,移出显示边框黄色  默认黄色
	 -->

	<img style = "display: block;" src="img/1.jpg" alt="" width="200" height="200">
	<img src="img/2.jpg" alt="" width="200" height="200">
	<img src="img/3.jpg" alt="" width="200" height="200">
	<img src="img/4.jpg" alt="" width="200" height="200">

</body>
</html>
  • 案例:鼠标移入移除改变图片的边框颜色----导航效果

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		*{margin: 0px;padding: 0px}
		ul{list-style: none}
		#oDiv{
			margin: 0 auto;
			width: 80%;
		}
		ul li{
			float: left
		}
		ul li a{
			display: inline-block;
			width: 103px;
			height: 30px;
			text-decoration: none;
			background-image: url('img/bg1.gif');
			margin-left: 1px;
			font-size: 12px;
			text-align: center;
			line-height: 30px;
			color: white;
		}
	</style>
	<script type="text/javascript">
		window.onload = function(){
			var aa = document.getElementsByTagName("a");
			for(var i = 0;i<aa.length;i++){
				aa[i].onmouseover = function(){
					this.style.color = 'yellow';
					this.style.backgroundImage = 'url(img/bg2.gif)';
				};

				aa[i].onmouseout = function(){
					this.style.color = 'white';
					this.style.backgroundImage = 'url(img/bg1.gif)';
				};
			}
		};


	</script>
</head>
<body>
	<div id="oDiv">
		<ul>
			<li><a href ="">皇帝</a></li>
			<li><a href ="">元帅</a></li>
			<li><a href ="">将军</a></li>
			<li><a href ="">老师</a></li>
			<li><a href ="">诗人</a></li>
		</ul>
	</div>
</body>
</html>

四:JavaScript 特效之四大家族    

  1. 三大系列:offset、scroll、client

  2. 事件对象:event(事件被触动时,鼠标和键盘的状态)(通过属性控制)

  • offset 系列

  • 偏移量  得到标签的大小以及标签距离网页两端的间距。      

  • 作用: 

  1.  获得元素距离带有定位父元素的位置(offsetLeft/offsetTop)

  2.  获得元素自身的大小(宽度高度)

  • 属性方法: 

  • offsetWidth|offsetHeight  宽度:width+边框+内填充( 获得元素自身的大小(宽度高度) ) 

  • offsetParent (获取指定元素的父级元素对象,如果父级对象没有定位直接指向body)

问题:style属性:只能调用行内样式,若要获取非行内式样式的属性,应该怎么办?

解决办法:通过currentStyle或者getComputedStyle解决非行内式样式的获取

  • 案例:演示offset 系列的属性方法

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		*{margin: 0px;padding: 0px;}
		div{
			width: 200px;
			height: 200px;
			background-color: red;
			border: 10px solid yellow;
			padding-left:10px;
			position: relative;
			top: 200px;
			left: 200px;
			overflow: auto;
		}
	</style>

	<script type="text/javascript">
		//方法:获取样式
		function getStyle(obj,name){//obj:操作哪一个标签 name:该标签的属性
		    if(obj.currentStyle){//兼容IE
				return obj.currentStyle[name];
		    }else{//兼容非IE浏览器---谷歌  火狐等等
				return getComputedStyle(obj,false)[name];
		    }
		}

		window.onload = function(){
			var oDiv = document.getElementById("oDiv");
			//获取div标签距离父标签的距离。
			console.log(oDiv.offsetLeft);
			console.log(oDiv.offsetTop);
			// offsetWidth|offsetHeight  宽度:width+边框+内填充
			console.log(oDiv.offsetWidth);
			console.log(oDiv.offsetHeight);
	
			//获取指定元素的父级元素对象,如果父级对象没有定位直接指向body
			console.log(oDiv.offsetParent);

			//获取被滚动条滚去的距离
			window.onscroll = function(){
				var scrollDemo = scroll();
				console.log(scrollDemo.left)
				console.log(scrollDemo.top)
			};

			//div的实际高度
			console.log("width: "+oDiv.scrollHeight);
			console.log("width: "+oDiv.scrollWidth);
		}


		/**
		 * scroll()方法的封装
		 */
		function scroll() {
			//直接return一个json对象
			return {
				"left": window.pageXOffset || document.documentElement.left || document.body.scrollLeft,
				"top": window.pageYOffset || document.documentElement.top || document.body.scrollTop
			};
		}

	</script>
</head>
<body>
	<script type="text/javascript">

		for(var i = 1;i<=1000;i++){
			document.write(i+"<br/>");
		}

	</script>
	<div id="oDiv">
		我是内容
		我是内容
		我是内容
		我是内容
		我是内容
		我是内容
		我是内容
	</div>
</body>
</html>
  • client 系列    

  • 客户端  得到屏幕的可视区宽度和高度

  • 作用:

  • 可以动态的得到该元素的边框大小、元素大小等

  • 属性方法:

  1. clientWidth:获取网页可视区域宽度

  2. clientHeight:获取网页可视区域高度

  3. clientX:鼠标距离可视区域左侧距离

  4. clientY:鼠标距离可视区域上侧距离

  5. clientTop / clientLeft  :   获取盒子的 border 宽高           

  • 案例:演示 client系列的属性方法

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		*{margin: 0px;padding: 0px;}
		div{
			width: 200px;
			height: 200px;
			background-color: red;
			border: 10px solid yellow;
			padding-left:10px;
			position: relative;
			top: 200px;
			left: 200px;
			overflow: auto;
		}
	</style>

	<script type="text/javascript">
		//方法:获取样式
		function getStyle(obj,name){//obj:操作哪一个标签 name:该标签的属性
		    if(obj.currentStyle){//兼容IE
				return obj.currentStyle[name];
		    }else{//兼容非IE浏览器---谷歌  火狐等等
				return getComputedStyle(obj,false)[name];
		    }
		}

		window.onload = function(){
			var oDiv = document.getElementById("oDiv");
			
			//clientTop获取上边框的大小
			console.log(oDiv.clientTop);
			//clientLeft获取左边框的大小
			console.log(oDiv.clientLeft);
			//获取指定元素的宽度(width+内填充)
			console.log(oDiv.clientWidth);
			//获取指定元素的宽度(width+内填充)
			console.log(oDiv.clientHeight);//200

			var clientDemo = client();
			console.log(clientDemo.width);//800指的时当前屏幕的宽度
			console.log(clientDemo.height);//149 指的时当前屏幕的可视区高度
			
		}


		/**
		 * client浏览器窗口可视区域兼容性
		 */
		function client() {
			if (window.innerWidth != null) // ie9 +  最新浏览器
			{
				return {
					width: window.innerWidth,
					height: window.innerHeight
				}
			} else if (document.compatMode === "CSS1Compat") // 标准浏览器
			{
				return {
					width: document.documentElement.clientWidth,
					height: document.documentElement.clientHeight
				}
			}
			return { // 怪异浏览器
				width: document.body.clientWidth,
				height: document.body.clientHeight

			}
		}
	</script>
</head>
<body>
	<script type="text/javascript">

		for(var i = 1;i<=1000;i++){
			document.write(i+"<br/>");
		}

	</script>
	<div id="oDiv">
		我是内容
		我是内容
		我是内容
		我是内容
		我是内容
		我是内容
		我是内容

		我是内容
		我是内容
		我是内容

	</div>
</body>
</html>
  • Scroll 系列

  • 滚动的 可以动态的得到该元素的大小、滚动距离等

  • 属性方法:

  • ScrollWidth 和 scrollHeight

  •  检测盒子的宽高(不包括border)

  • 盒子内容的宽高(不包括border)

  • scrollTop / scrollLeft

  • 网页,被浏览器遮挡的头部和左边部分,被卷去的头部和左边部分。

  • 兼容性问题解决方法:

  1. ① 未声明 DTD 时(谷歌只认识他)

  2. document.body.scrollTop

  3. ② 已经声明DTD 时(IE678只认识他)

  4.  document.documentElement.scrollTop

  5.  ③ 火狐/谷歌/ie9+以上支持的

  6. window.pageYOffset

  • 案例:演示 Scroll系列的属性方法

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		#oDiv{
			width: 100px;
			height: 100px;
			background-color: red;
			position: absolute;
			top: 100px;
			left:500px;
		}

	</style>
	<script type="text/javascript">

		//方法:获取非行内式样式
		function getStyle(obj,name){//obj:操作哪一个标签 name:该标签的属性
		    if(obj.currentStyle){//兼容IE
				return obj.currentStyle[name];
		    }else{//兼容非IE浏览器---谷歌  火狐等等
				return getComputedStyle(obj,false)[name];
		    }
		}

		/**
		 * scroll()方法的封装
		 */
		function scroll() {
			//直接return一个json对象
			return {
				"left": window.pageXOffset || document.documentElement.left || document.body.scrollLeft,
				"top": window.pageYOffset || document.documentElement.top || document.body.scrollTop
			};
		}
			window.onload = function(){
				//1.获取div盒子本身的宽度和高度
				var oDiv = document.getElementById("oDiv");
				//获取盒子本身的高度
				var height = oDiv.offsetHeight;
				// alert(height);
				//获取盒子距离父级元素的高度
				var top = getStyle(oDiv,"top");
				// alert(top);
				//设置滚动条的监听事件
				window.onscroll = function(){
					//得到滚动条滚去的距离
					var scrollDemo = scroll();
					// console.log(scrollDemo.top);
					
					//定义一个变量保存总距离
					var sum =  parseInt(top)+scrollDemo.top;
					console.log(sum);

					oDiv.style.top = sum+'px';
				};

			}


	</script>
</head>
<body>
	<script type="text/javascript">
		for(var i = 1;i<=1000;i++){
			document.write(i+"<br/>");
		}
	</script>
	<div id="oDiv">

	</div>
</body>
</html>
  • 案例:实现圆球滚圈效果

 

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		#oDiv{
			width: 100px;
			height: 100px;
			background-color: red;
			position: absolute;
			top: 100px;
			left:0px;
			/*盒子的圆弧度*/
			border-radius: 100%;
		}
	</style>
	<script type="text/javascript">
		//方法:获取非行内式样式
		function getStyle(obj,name){//obj:操作哪一个标签 name:该标签的属性
		    if(obj.currentStyle){//兼容IE
				return obj.currentStyle[name];
		    }else{//兼容非IE浏览器---谷歌  火狐等等
				return getComputedStyle(obj,false)[name];
		    }
		}
		/**
		 * client浏览器窗口可视区域兼容性
		 */
		function client() {
			if (window.innerWidth != null) // ie9 +  最新浏览器
			{
				return {
					width: window.innerWidth,
					height: window.innerHeight
				}
			} else if (document.compatMode === "CSS1Compat") // 标准浏览器
			{
				return {
					width: document.documentElement.clientWidth,
					height: document.documentElement.clientHeight
				}
			}
			return { // 怪异浏览器
				width: document.body.clientWidth,
				height: document.body.clientHeight

			}
		}
	window.onload = function(){
		var oDiv = document.getElementById("oDiv");
		var flag = true;
		var flag2 = true;
		window.setInterval(function(){
			var left = parseInt(getStyle(oDiv,"left"));
			var top = parseInt(getStyle(oDiv,"top"));

			var width = (document.documentElement.clientWidth || document.body.clientWidth)-oDiv.offsetWidth;

			var height = (document.documentElement.clientHeight || document.body.clientHeight)-oDiv.offsetHeight;
			
			if(flag == true){
				left+=5;
			}
			if(left >= width){
				flag = false;
			}
			if(flag == false){
				left-=5;
			}
			if(left<=0){
				flag=true;
			}

			if(flag2 == true){
				top+=5
			}
			if(top >= height){
				flag2 = false;
			}
			if(flag2 == false){
				top-=5;
			}
			if(top<=0){
				flag2=true;
			}


			
			oDiv.style.left = left+'px';
			oDiv.style.top = top+'px';

		},50);
	};

	</script>
</head>
<body>
	<div id="oDiv">


	</div>
</body>
</html>

五:三大家族区别

  •  Width 和 height

  1. clientWidth  = width  + padding

  2. clientHeight  = height + padding

  3. offsetWidth  = width  + padding + border

  4. offsetHeight  = height + padding + border

  5. scrollWidth   = 内容宽度(不包含border)

  6. scrollHeight  = 内容高度(不包含border)

  • top 和 left

  1. offsetTop/offsetLeft :

  2. 调用者:任意元素。(盒子为主)

  3. 作用:距离父系盒子中带有定位的距离。

  4. scrollTop/scrollLeft:(盒子也可以调用,必须有滚动条)

  5. 调用者:document.body.scrollTop/.....(window)

  6.  作用:浏览器无法显示的部分(被卷去的部分)

  7. clientY/clientX:(clientTop/clientLeft 值的是border)

  8. 调用者:event.clientX(event)

  9. 作用:鼠标距离浏览器可视区域的距离(左、上) 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值