gpt4 book ai didi

Javascript onclick,如何为某些元素设置不同的 alpha?

转载 作者:搜寻专家 更新时间:2023-10-31 22:37:29 24 4
gpt4 key购买 nike

单击事件会更改每一列的背景颜色并且它有效。但是我想要不同的 html-body 背景颜色(只有不透明度)。现在它为 body.background(最后一行)提供与其他列相同的颜色。相反,我希望主体具有相同的颜色,但不透明度为 0.5,因此顶部的不透明度为 1 的元素会很好地脱颖而出。

function BackgroundColorChange(){
// the main opacity level
var a = 1;
// some colors chosen for DOM elements backgrounds
var blue = 'rgba(51, 102, 153,'+a+')';
var grey = 'rgba(66, 82, 102,'+a+')';
var darkgreen = 'rgba(25, 102, 102,'+a+')';
var lightgreen = 'rgba(62, 116, 89,'+a+')';
var brown = 'rgba(96, 86, 57,'+a+')';
var purple = 'rgba(66, 36, 51,'+a+')';

var colorSelection = [blue, grey, darkgreen, lightgreen, brown, purple];

// pick the random color for background as the event is clicked
var color = colorSelection[Math.floor(Math.random() * colorSelection.length)];

// the elements i want to have 'opacity = 1' are selected here
var subsection = document.querySelectorAll(".childElement-column-photo, .childElement-sub-section, .sub-section, .modal-header, .modal-footer");

for (var i = 0; i < subsection.length; i++) {
subsection[i].style.backgroundColor = color;
}

a = 0.5; //? ? ? ? ?

// I want this body element to have the same color but with 0.5 opacity
document.body.style.backgroundColor = color;
}

我尝试过为不透明度定义“a”,但没有奏效。

HTML:这主要是我的 html 的标记方式。

    <a onclick="BackgroundColorChange();" href="#">ChangeColor</a>

<body onload="setInterval(BackgroundColorChange(), 50000)">

// there are many elements with this class name
<div class="childElement-column-photo">

最佳答案

我认为以下是最佳解决方案:

尝试将 rgb 颜色保存为数组并即时构建您的 css 属性。

function buildRgbaCSSProperty(color, alpha) {
return 'rgba(' + color[0] + ', ' + color[1] + ', ' + color[2] + ', '+ alpha + ')';
}

function BackgroundColorChange(){
// the main opacity level
var a = 1;
// some colors chosen for DOM elements backgrounds
var blue = [51, 102, 153];
var grey = [66, 82, 102];
var darkgreen = [25, 102, 102];
var lightgreen = [62, 116, 89];
var brown = [96, 86, 57];
var purple = [66, 36, 51];

var colorSelection = [blue, grey, darkgreen, lightgreen, brown, purple];

// pick the random color for background as the event is clicked
var color = colorSelection[Math.floor(Math.random() * colorSelection.length)];

// the elements i want to have 'opacity = 1' are selected here
var subsection = document.querySelectorAll(".childElement-column-photo, .childElement-sub-section, .sub-section, .modal-header, .modal-footer");

for (var i = 0; i < subsection.length; i++) {
subsection[i].style.backgroundColor = buildRgbaCSSProperty(color, a);
}

a = 0.5; //? ? ? ? ?

// I want this body element to have the same color but with 0.5 opacity
document.body.style.backgroundColor = buildRgbaCSSProperty(color, a);
}

关于Javascript onclick,如何为某些元素设置不同的 alpha?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46245277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com