css父级/子级出现问题
Trouble with the css parent/child
我在css parent/child:中遇到问题
bg.className = "centrer";
bg.style.backgroundColor = "blue";
var container = document.createElement("div");
container.className = 'container';
container.style.height = '100px';
container.style.width = '110px';
var inner = document.createElement("div");
inner.className = 'inner';
var title = document.createElement("p");
title.textContent = "Test";
title.className = 'centrerTitre';
title.style.color = 'black';
inner.appendChild(title);
container.appendChild(inner);
bg.appendChild(container);
document.getElementById('bar').addEventListener("change", function(){
bg.style.opacity = this.value/100;
container.style.opacity = 1;
document.getElementById('valeurBar').innerHTML = this.value + "%";
}, false);
.centrer {
display: block;
margin: auto;
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
height : 250px;
width : 500px;
text-align : center;
}
.container:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.container {
position:absolute !important;
border:3px solid black;
opacity : 1;
}
.centrer > div {
opacity:1;
}
.inner {
display: inline-block;
vertical-align: middle;
text-align:center;
width:100%;
}
.centrerTitre{
font-size: 25;
font-weight: bold;
color : white;
margin : auto;
text-align : center;
padding-left : 4px;
padding-right : 4px;
}
Opacity : <input type='range' min='0' max='100' value='100' style='width:250px' id='bar'><span id='valeurBar'>100%</span>
<div id="bg"></div>
我有一个div子级到一个div父级。我们可以使用值栏更改父对象的不透明度。但当我们这样做的时候,孩子的不透明性也会改变。我尝试将其放入css:.centrer>div{opacity:1;}中,但没有成功,我还尝试将container.style.opacity=1;但这也不起作用。
如何在不更改子对象不透明度的情况下单独更改父对象的不透明度?
感谢您的关注。
编辑:很抱歉,为了你的理解,我简化了我的问题,通常背景是一个图像,而不仅仅是一种颜色。如何使用图像来完成此操作?(基本上,我走这条路是因为我不知道是否可以上传图像…)
但您可以将背景颜色更改为RGBA
值以获得以下内容:
bg.className = "centrer";
bg.style.backgroundColor = "blue";
var container = document.createElement("div");
container.className = 'container';
container.style.height = '100px';
container.style.width = '110px';
var inner = document.createElement("div");
inner.className = 'inner';
var title = document.createElement("p");
title.textContent = "Test";
title.className = 'centrerTitre';
title.style.color = 'black';
inner.appendChild(title);
container.appendChild(inner);
bg.appendChild(container);
document.getElementById('bar').addEventListener("change", function(){
bg.style.backgroundColor = "rgba(0,0,255," + this.value/100 + ")";
container.style.opacity = 1;
document.getElementById('valeurBar').innerHTML = this.value + "%";
}, false);
.centrer {
display: block;
margin: auto;
/* position: absolute; */
top: 0; bottom: 0; left: 0; right: 0;
height : 250px;
width : 500px;
text-align : center;
}
.container:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.container {
position:absolute !important;
border:3px solid black;
background: blue;
}
.inner {
display: inline-block;
vertical-align: middle;
text-align:center;
width:100%;
}
.centrerTitre{
font-size: 25;
font-weight: bold;
color : white;
margin : auto;
text-align : center;
padding-left : 4px;
padding-right : 4px;
}
Opacity : <input type='range' min='0' max='100' value='100' style='width:250px' id='bar'><span id='valeurBar'>100%</span>
<div id="bg"></div>
如果更改父对象的不透明度,子对象将受到影响。但是,您可以做一个技巧,就是更改父对象的背景不透明度。
EDIT:图像背景解决方案
如果你的背景是一个图像,你可以创建一个图像来填充div作为背景,并像这样改变它的不透明度。演示
HTML
<input type='range' min='0' max='100' value='100' style='width:250px' id='bar'><span id='valeurBar'>100%</span>
JS-
var bg = document.getElementById('bg');
bg.className = "centrer";
bg.style.backgroundColor = "blue";
var imageBackground = document.createElement("div");
imageBackground.className = 'image-background';
imageBackground.style.height = '100%';
imageBackground.style.width = '100%';
bg.appendChild(imageBackground);
var container = document.createElement("div");
container.className = 'container';
container.style.height = '100px';
container.style.width = '110px';
var inner = document.createElement("div");
inner.className = 'inner';
var title = document.createElement("p");
title.textContent = "Test";
title.className = 'centrerTitre';
title.style.color = 'black';
inner.appendChild(title);
container.appendChild(inner);
bg.appendChild(container);
document.getElementById('bar').addEventListener("change", function(){
imageBackground.style.opacity = this.value/100;
document.getElementById('valeurBar').innerHTML = this.value + "%";
}, false);
CSS
.centrer {
display: block;
margin: auto;
/* position: absolute; */
top: 0; bottom: 0; left: 0; right: 0;
height : 250px;
width : 500px;
text-align : center;
position:relative;
}
.container:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.container {
position:absolute !important;
border:3px solid black;
background: blue;
}
.inner {
display: inline-block;
vertical-align: middle;
text-align:center;
width:100%;
}
.centrerTitre{
font-size: 25;
font-weight: bold;
color : white;
margin : auto;
text-align : center;
padding-left : 4px;
padding-right : 4px;
}
.image-background{
position:absolute;
background: url("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRCbA4ze7ExrnpgnTxMSBiifLg_TLz3HRLycnlmTWHkvMwcY5X2nZrI_w");
}
恐怕在更改父级不透明度时,也会更改所有子级节点。
我会做的是将"bd"div与容器分离,然后重叠位置。。。像这样的东西:
bg.className = "centrer";
bg.style.backgroundColor = "blue";
var container = document.createElement("div");
container.className = 'container';
container.style.backgroundColor = bg.style.backgroundColor;
container.style.height = '100px';
container.style.width = '110px';
var inner = document.createElement("div");
inner.className = 'inner';
var title = document.createElement("p");
title.textContent = "Test";
title.className = 'centrerTitre';
title.style.color = 'black';
inner.appendChild(title);
container.appendChild(inner);
bg.parentNode.appendChild(container);
document.getElementById('bar').addEventListener("change", function(){
bg.style.opacity = this.value/100;
container.style.opacity = 1;
document.getElementById('valeurBar').innerHTML = this.value + "%";
}, false);
.centrer {
display: block;
margin: auto;
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
height : 250px;
width : 500px;
text-align : center;
}
.container:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.container {
display: block;
margin: auto;
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
border:3px solid black;
opacity : 1;
}
.centrer > div {
opacity:1;
}
.inner {
display: inline-block;
vertical-align: middle;
text-align:center;
width:100%;
}
.centrerTitre{
font-size: 25;
font-weight: bold;
color : white;
margin : auto;
text-align : center;
padding-left : 4px;
padding-right : 4px;
}
Opacity : <input type='range' min='0' max='100' value='100' style='width:250px' id='bar'><span id='valeurBar'>100%</span>
<div id="bg"></div>
相关文章:
- jQuery最近父级的数据属性选择器
- 如何通过溢出来判断元素被切断了多少像素:隐藏在父级上
- 来自文档或下一个静态父级的事件委派
- 从中关闭子窗口'的父级
- JavaScript:单击时相对于父级增加变量值
- 通过从节点父级获取所有子级对节点进行排序(获取子级数组)
- 阻止对父级的相同域Iframe访问
- 嵌套对象文本访问父级
- React.js:将事件从父级附加到子级
- 在React JS中将道具传递给父级
- 从子级(iframe)到父级的跨域通信不起作用
- React:用父级状态更改更新子级
- 100%的父图像.父级是固定位置的
- 当我将子项从一个父项移动/复制到另一个父级时,如何重新计算显示
- 从父级触发弹出窗口中的事件
- Jquery/Javascript—查找父级's的兄弟姐妹's儿童'的孩子
- AngularJS指令监视父级大小的更改
- 在父级上使用转换:翻译时修复元素的问题
- css父级/子级出现问题
- Javascript原型继承:当父级和子级接收相同的对象作为参数时的问题