JavaScript与CSS的交互(五)

目录

一,样式表

1.样式表中的三种选择器

 2.样式表中的常见属性

边框属性

边界属性

填充属性

文本属性 

 背景属性

2.三种样式表

 二,具体案例

1.鼠标悬停图片放大效果

2.实现广告悬停的效果

3.增加边框


 

一,样式表

1.样式表中的三种选择器

  • ID选择器:# a{}
  • 类选择器:.a{}
  • 标签选择器:input{}

 2.样式表中的常见属性

  • 边框属性

边框属性描述
border-width设置边框的宽度
border-color设置边框的颜色
border-style设置边框的样式

  • 边界属性

边界属性描述
margin-left分别设置元素中的左、右、上、下外边距
margin-right
margin-top
margin-bottom

  • 填充属性

填充属性描述
padding-left分别设置元素中的左、右、上、下内边距
padding-right
padding-top
padding-bottom
  • 文本属性 

文本属性描述
font-size字体大小
font-family字体类型
font-style字体样式
font-color字体颜色
text-align文本对齐
line-height行高
  •  背景属性

背景属性描述
background-color设置背景颜色
background-image设置背景图像
background-repeat设置一个图片的平铺方式

2.三种样式表

样式表语法格式优点缺点使用情况控制范围
行内<div style="..."></div>书写方便,权重高没有实现样式和结构相分离较少控制一个标签
内部<style>...</style>部分结构和样式相分离没有彻底分离较多控制一个页面
外部<link rel="stylesheet" href="..." />完全实现结构和样式分离需要引入最多强烈推荐控制整个站点

 

 二,具体案例

1.鼠标悬停图片放大效果

原理:先排版好图片,再编辑函数设置div的位置的图片路径,当鼠标移入小图片的时候出发鼠标移入事件,更换图片的路径。代码如下:

<!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>
		<!-- 编辑内部样式,将文本的对齐形式设置为居中对齐 -->
		<style type="text/css">
			td {
				text-align: center;
			}
		</style>
		<script type="text/javascript">
			//传入一个标签和图片的路径
			function over(obj_small, obj_big) {
				obj_small.style.border = "solid 1px #ff0000"; //设置标签的边框
				document.getElementById("big").src = "images/" + obj_big; //根据ID拿到元素,再设置图片的路径
			}
			//创建设置边框的函数(当鼠标移除的时候就将边框的宽度设置为0)
			function out(obj_small) {
				obj_small.style.border = 0;
			}
		</script>
	</head>

	<body>
		<!-- 排版 -->
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td colspan="5"><img src="images/show1_big.jpg" id="big" /></td><!-- 设置跨单元格5 -->
			</tr>
			<tr>
				<td style="height:60px;"><img src="images/show1.jpg" onmouseover="over(this,'show1_big.jpg')"
						onmouseout="out(this)" /></td>
				<td><img src="images/show2.jpg" onmouseover="over(this,'show2_big.jpg')" onmouseout="out(this)" /></td>
				<td><img src="images/show3.jpg" onmouseover="over(this,'show3_big.jpg')" onmouseout="out(this)" /></td>
				<td><img src="images/show4.jpg" onmouseover="over(this,'show4_big.jpg')" onmouseout="out(this)" /></td>
				<td><img src="images/show5.jpg" onmouseover="over(this,'show5_big.jpg')" onmouseout="out(this)" /></td>
			</tr>
		</table>

	</body>
</html>

2.实现广告悬停的效果

原理:当滑动滚动条的时候,设置广告位置(位置为滚动条滑动的距离+广告的原位置),代码如下:

//实现悬停效果

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>实现图片悬停的效果</title>
		<style type="text/css">
			div {
				border: 2px solid bisque;
				position: absolute;
				right: 20px;
				top: 20px;
				/* 设置绝对布局 */
			}
		</style>
	</head>
	<body>
		<div id="advertise">
			<img width="100px" height="100px" src="imgs/1.gif" />
		</div>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<p>hello world!</p>
		<script type="text/javascript">
			//创建图片移动的函数
			window.onscroll = () => {
                // 设置竖直位置
				advertise.style.top = document.documentElement.scrollTop + 20 + "px";
				// 设置水品位置
				advertise.style.left = document.documentElement.scrollLeft - 20 + "px";
			}
		</script>
	</body>
</html>

3.增加边框

原理:给按钮增加点击事件,当被点击的时候就将img的标签样式改变,给大家举例了三种实现方式,代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    //编辑样式,采用类选择器
    <style>
        .a{
            border: 10px solid green;//设置边框
            box-shadow: 0px 0px 10px yellow;//设置阴影
        }
    </style>
</head>
<body>
<img id="img1" src="img/1.gif" alt="">
<img id="img2" src="img/2.gif" alt="">
<img id="img3" src="img/3.gif" alt="">
<p>
//设置点击按钮,增加点击事件
    <button onclick="fn1()">点我添加边框</button>
    <button onclick="fn2()">点我添加边框</button>
    <button onclick="fn3()">点我添加边框</button>
</p>
<script>
//创建函数
    function fn1() {
        //操作css的第一种方式:  直接操作style
        m1.style.border="5px solid black"//设置边框
        m1.style.width="40px"//设置宽度
        m1.style.opacity=.5//设置透明度
    }

    function fn2(){
        m2.setAttribute("class","a")//使用类选择器
    }

    function fn3(){
        //class是关键字
        //标签中的class属性在js中都叫做className
        m3.className="a"
    }
</script>
</body>
</html>

今天的分享到此为止,下期给大家带来更多精彩案例!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一麟yl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值