为什么我把background设置了0.5,opacity设置0.5,浏览器打开是没有内容显示的?

来源:4-1 导航菜单的结构

choyuky

2018-01-19 15:06:00

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JavaScript实现轮播特效</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="main" id="main">
<!--导航菜单-->
<div class="menu-box"></div>
<!--主菜单-->
<div class="menu-content">
<div class="menu-item">
<a href="">
<span>手机、配件</span>
<i>&#xe665;</i>
</a>
</div>
<div class="menu-item">
<a href="">
<span>电脑</span>
<i>&#xe665;</i>
</a>
</div>
<div class="menu-item">
<a href="">
<span>家用电器</span>
<i>&#xe665;</i>
</a>
</div>
<div class="menu-item">
<a href="">
<span>家具</span>
<i>&#xe665;</i>
</a>
</div>
</div>

<!-- 图片轮播 -->
<div class="banner" id="banner">
<a href="">
<div class="banner-slide slide1 slide-active"></div>
</a>
<a href="">
<div class="banner-slide slide2"></div>
</a>
<a href="">
<div class="banner-slide slide3"></div>
</a>
</div>

<!-- 上一张、下一张按钮 -->
<a href="JavaScript:void(0)" class="button prev" id="prev"></a>
<a href="JavaScript:void(0)" class="button next" id="next"></a>

<!-- 圆点导航 -->
<div class="dots" id="dots">
<span class="active"></span>
<span></span>
<span></span>
</div>
</div>
<script src="js/js.js"></script>
</body>
</html>
*{
	margin: 0;
	padding: 0;
}

ul{
	list-style: none;
}

a:link,a:visited{
text-decoration: none;
}

body{
	font-family: "微软雅黑";
	color: #14191e;
}

.main{
	width: 1200px;
	height: 460px;
	margin: 30px auto;
	overflow: hidden;
	position: relative;
}
.banner{
	width: 1200px;
	height: 460px;
	overflow: hidden;
	position: relative;
}
.banner-slide{
	width: 1200px;
	height: 460px;
	position: absolute;
	background-repeat: no-repeat;
	display: none;
}
.slide1{
	background-image: url(../img/bg1.jpg);
}
.slide2{
	background-image: url(../img/bg2.jpg);
}
.slide3{
	background-image: url(../img/bg3.jpg);
}
.slide-active{
	display: block;
}
.button{
	position: absolute;
	width: 40px;
	height: 80px;
	left: 244px;
	top: 50%;
	margin-top: -40px;
	background: url(../img/arrow.png) no-repeat center center;
}
.button:hover{
	background-color: #333;
	opacity: 0.2;
	filter: alpha(opacity=20);

}
.prev{
	transform: rotate(180deg);
}
.next{
	left: auto;
	right: 0;
}
.dots{
	position: absolute;
	right: 24px;
	bottom: 24px;
	text-align: right;
}
.dots span{
	display: inline-block;
	width: 12px;
	height: 12px;
	line-height: 12px;
	border-radius: 50%;
	background: rgba(7,17,27,0.4);
	box-shadow: 0 0 0 2px rgba(255,255,255,0.4) inset;/*inset内置描边*/
	margin-left: 8px;
	cursor: pointer;

}
.dots span.active{
	background: #fff;
	box-shadow: 0 0 0 2px rgba(27,17,27,0.4) inset;/*inset内置描边*/

}

/*导航菜单*/
.menu-box{
	width: 244px;
	height: 460px;
	position: absolute;
	left: 0;
	top: 0;
	background: rgb(7,17,27,0.5);
	opacity: 0.5;
	z-index: 1;
}
.menu-content{
	width: 244px;
	height: 454px;
	position: absolute;
	left: 0;
	top: 0;
	z-index: 2;
	padding-top: 6px;
}
.menu-item{

}
//封装一个代替getElementById()的方法
function byId(id) {
	return typeof(id) === "string"?document.getElementById(id):id;
}

//全局变量
var index = 0,
	timer = null,
	pics = byId("banner").getElementsByTagName('div'),
	dots = byId("dots").getElementsByTagName('span'),
	prev = byId("prev"),
	next = byId("next"),
	len = pics.length;

function slideImg() {
	var main = byId("main");
	//滑过清除定时器,离开继续
	main.onmouseover = function () {
		//滑过清除定时器
		if (timer) clearInterval(timer);
	}
	
	main.onmouseout = function () {
		timer = setInterval(function(){
			index++;  //len = 3
			if (index>=len) {
				index = 0;
			}
			//切换图片
			changeImg();
		},3000);
	}

	//自动在main上触发鼠标离开的事件
	main.onmouseout();

	//遍历所有点击,且绑定点击事件,点击圆点切换图片
	for (var d = 0 ;d < len; d++) {
		//给所有span添加一个id的属性,值为d,作为当前span的索引
		dots[d].id =d;
		dots[d].onclick = function(){
			//改变index为当前span的id值
			index = this.id;

			//条用changeImg,实现切换图片
			changeImg();
		}
	}

	//下一张
	next.onclick = function () {
		index++;
		if (index>= len) index=0;
		changeImg();
	}

	//上一张
	next.onclick = function () {
		index--;
		if (index<0) index=len-1;
		changeImg();
	}

}

//切换图片
function changeImg() {
	//需要遍历banner下多有的div及dots下所有的span,将div隐藏,将span清除类
	for (var i = 0; i <len; i++) {
		pics[i].style.display = 'none';
		dots[i].className = "";
	}
	//根据index索引找到当前div和当前span,将其显示出来和设为当前
	pics[index].style.display = 'block';
	dots[index].className = "active";
}


slideImg();


http://img.mukewang.com/climg/5a61999e0001882110980562.jpg

background设置透明度0.5,opacity设置0.5,没有效果显示



http://img.mukewang.com/climg/5a6198bc0001c51210400569.jpg

background没有设置透明度,opacity设置0.5,效果出来了



写回答

2回答

怎么都被占用了呢

2018-01-19

设置背景色的时候,想要设置透明度,要使用rgba设置。

http://img.mukewang.com/climg/5a61a4780001670805920278.jpg

0
hhoyuky
h 哦哦~明白了,我设成了rgb,谢谢老师!
h018-01-19
共1条回复

怎么都被占用了呢

2018-01-19

将你的代码完整的放上来吧,方便帮你找问题所在。

0
hhoyuky
h 重新修改了下问题了,麻烦老师帮我看看是什么问题
h018-01-19
共1条回复

0 学习 · 36712 问题

查看课程