gpt4 book ai didi

javascript - 将按钮更改为不同的背景 onClick

转载 作者:行者123 更新时间:2023-11-28 17:46:17 29 4
gpt4 key购买 nike

我试图让这个按钮更改为不同的背景,“线性渐变”,后面有一个图像。这是一个音频播放按钮。

你按下它,它就会继续,如果熄灭,你再按一次。

初始类别 = 您首先看到的内容
(只有线性渐变背景,后面没有图像)

暂停类(class) = 单击它后您看到的内容
(透明线性渐变背景,后面有图像)

播放类(class) = 再次点击后看到的内容
(透明线性渐变背景,后面有图像)

暂停类(class) = 单击它后您看到的内容
(透明线性渐变背景,后面有图像)

svg 按钮从初始变为暂停、播放、返回到暂停、然后播放,但透明线性渐变背景与初始类背景相比没有变化。

有人可以帮我让这段代码正常工作吗?我已经尝试了一切,但没能让它发挥作用。它会打开和关闭,但线性背景类(暂停、播放)及其后面的背景图像这些背景在您单击后永远不会出现,它只是停留在初始类上> 背景,并且永远不会改变。这就是问题所在。

我忘了提及,我需要能够将此代码放置在某个地方两次。这是透明线性渐变背景后面的背景图像。初始类没有获得图像,只有后两个、播放和暂停。目前不在完整代码中,我正在尝试找出将其放置在哪里。

, url('http://via.placeholder.com/266x266');

这就是(初始、暂停、播放)按钮独立于按钮代码设置的方式。

https://jsfiddle.net/k9qscyda/4/

初始:

<svg class="initial" style=" border: 3px solid #0059dd;padding:76px 85px 76px 85px;cursor: pointer;background-image: linear-gradient( to right,#000000 83px,#0059dd 83px, #0059dd 86px, #000000 86px, #000000  174px, #0059dd 174px, #0059dd 177px, #000000 177px );"
width="90" height="108" viewBox="0 -3 85 120">
<path fill="currentColor" style="stroke: #e77d19; stroke-width:3px;color:black; " d="M81 44.6c5 3 5 7.8 0 10.8L9 98.7c-5 3-9 .7-9-5V6.3c0-5.7 4-8 9-5l72 43.3z"></path>
</svg>

暂停:

<svg class="pause" style="border: 3px solid #e77d19; padding:94px 100px 94px 100px;cursor: pointer;background: linear-gradient( to right,transparent 83px,#e77d19 83px, #e77d19 86px, transparent 86px, transparent 174px, #e77d19 174px, #e77d19 177px, transparent 177px ), url('http://via.placeholder.com/266x266');"
width="60" height="72" viewBox="0 0 16 14">
<path d="M12.945.38l-.652.762c1.577 1.462 2.57 3.544 2.57 5.858 0 2.314-.994 4.396-2.57 5.858l.65.763c1.79-1.644 2.92-3.997 2.92-6.62S14.735 2.024 12.945.38zm-2.272 2.66l-.65.762c.826.815 1.34 1.947 1.34 3.198 0 1.25-.515 2.382-1.342 3.2l.652.762c1.04-1 1.69-2.404 1.69-3.96 0-1.558-.65-2.963-1.69-3.963zM0 4v6h2.804L8 13V1L2.804 4H0zm7-1.268v8.536L3.072 9H1V5h2.072L7 2.732z"
fill="#1ed760 " fill-rule="evenodd"></path>

播放:

<svg class="play" style="border: 3px solid #e77d19;padding:94px 100px 94px 100px;cursor: pointer;background: linear-gradient( to right,transparent 83px,#e77d19 83px, #e77d19 86px, transparent 86px, transparent 174px, #e77d19 174px, #e77d19 177px, transparent 177px ), url('http://via.placeholder.com/266x266');http://via.placeholder.com/150x150"
width="60" height="72" viewBox="0 0 16 14">
<path d="M12.945.38l-.652.7623zM0 4v6h2.804L8 13V1L2.804 4H0zm7-1.268v8.536L3.072 9H1V5h2.072L7 2.732z" fill="#1ed760 " fill-rule="evenodd"></path>
</svg>

完整代码如下

https://jsfiddle.net/31exzq4y/5/

这是它的工作顺序。

class="初始"


class="pause"


class="play"


代码:

<button id="playButton2" style="display:block; width: 266px; height: 266px; cursor: pointer;background-image: linear-gradient( to right,#000000 83px,#0059dd 83px, #0059dd 86px, #000000 86px, #000000  174px, #0059dd 174px, #0059dd 177px, #000000 177px ); border: 3px solid #0059dd;"
onclick="
var button = document.getElementById('playButton2');
var player = document.getElementById('player2');
document.querySelector('#playButton2 .initial').style.display='none';
document.querySelector('#playButton2 .pause').style.display='none';
document.querySelector('#playButton2 .play').style.display='none';
player.volume=1.0; if (player.paused) {
playButton2.style.background = 'background: linear-gradient( to right,transparent 83px,#e77d19 83px, #e77d19 86px, transparent 86px, transparent 174px, #e77d19 174px, #e77d19 177px, transparent 177px )';
playButton2.style.padding = '94px 100px 94px 100px';
playButton2.style.cursor = 'pointer';
playButton2.style.border='3px solid #e77d19';
playButton2.style.backgroundRepeat = 'no-repeat';
playButton2.style.backgroundPosition = 'center';
document.querySelector('#playButton2 .pause').style.display='inline-block';
player.play();
} else {
playButton2.style.background = 'background: linear-gradient( to right,transparent 83px,#e77d19 83px, #e77d19 86px, transparent 86px, transparent 174px, #e77d19 174px, #e77d19 177px, transparent 177px )';
playButton2.style.border='3px solid #e77d19';
playButton2.style.padding = '94px 100px 94px 100px';
playButton2.style.cursor = 'pointer';
playButton2.style.backgroundRepeat = 'no-repeat';
playButton2.style.backgroundPosition = 'center';
document.querySelector('#playButton2 .play').style.display='inline-block';
player.pause();
}">

<svg class="pause" style="display: none;" width="60" height="72" viewBox="0 0 16 14">
<path d="M12.945.38l-.652.762c1.577 1.462 2.57 3.544 2.57 5.858 0 2.314-.994 4.396-2.57 5.858l.65.763c1.79-1.644 2.92-3.997 2.92-6.62S14.735 2.024 12.945.38zm-2.272 2.66l-.65.762c.826.815 1.34 1.947 1.34 3.198 0 1.25-.515 2.382-1.342 3.2l.652.762c1.04-1 1.69-2.404 1.69-3.96 0-1.558-.65-2.963-1.69-3.963zM0 4v6h2.804L8 13V1L2.804 4H0zm7-1.268v8.536L3.072 9H1V5h2.072L7 2.732z"
fill="#1ed760 " fill-rule="evenodd"></path>
</svg>

<svg class="play" style="display: none;" width="60" height="72" viewBox="0 0 16 14">
<path d="M12.945.38l-.652.7623zM0 4v6h2.804L8 13V1L2.804 4H0zm7-1.268v8.536L3.072 9H1V5h2.072L7 2.732z" fill="#1ed760 " fill-rule="evenodd"></path>
</svg>

<svg class="initial" width="90" height="108" viewBox="0 -3 85 120">
<path fill="currentColor" style="stroke: #e77d19; stroke-width:3px;color:black; " d="M81 44.6c5 3 5 7.8 0 10.8L9 98.7c-5 3-9 .7-9-5V6.3c0-5.7 4-8 9-5l72 43.3z"></path>
</svg>
</button>

<audio id="player2" style="display:none;">
<source src='http://hi5.1980s.fm/;' type='audio/mpeg'></source>
</audio>

最佳答案

您尝试将背景设置为无效值,因此它会被忽略。更改行以删除额外的 background: text:

fiddle :https://jsfiddle.net/31exzq4y/6/

具体变化是:

from: playButton2.style.background = 'background: linear-gradient( [...etc];
to: playButton2.style.background = 'linear-gradient( [...etc];

对于 else block 也是如此。

这是背景图片:https://jsfiddle.net/31exzq4y/7/

您需要在网址前面添加 https:// 并转义引号。

关于javascript - 将按钮更改为不同的背景 onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46644841/

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