簡體   English   中英

我將如何在JavaScript中設置CSS漸變背景?

[英]How would I go about setting a CSS gradient background in JavaScript?

這里描述 CSS漸變,但是我不知道如何在JavaScript中為這些屬性選擇。 如果可能的話,我寧願不使用jQuery。

編輯:僅執行以下操作似乎無效...

document.getElementById("selected-tab").style.background = "#860432";
document.getElementById("selected-tab").style.background = "-moz-linear-gradient(#b8042f, #860432)";
document.getElementById("selected-tab").style.background = "-o-linear-gradient(#b8042f, #860432)";
document.getElementById("selected-tab").style.background = "-webkit-gradient(linear, 0% 0%, 0% 100%, from(#b8042f), to(#860432))";
document.getElementById("selected-tab").style.background = "-webkit-linear-gradient(#b8042f, #860432)";

我不是JS專家,但是我的猜測是您的設置會相互覆蓋,因此您可能想為此創建一個CSS類選擇器,例如.gradientBackground並查看以下鏈接:

使用JavaScript更改元素的類

如果您不想使用jQuery,則如何應用樣式? 您應該創建一個包含每個瀏覽器漸變的類或ID。 然后使用jQuery將此類設置為ID“ selected-tab”

.someGradient{
    background: #1e5799; /* Old browsers */
    background: -moz-linear-gradient(top,  #1e5799 0%, #2989d8 50%, #207cca 51%, #7db9e8 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1e5799), color-stop(50%,#2989d8), color-stop(51%,#207cca), color-stop(100%,#7db9e8)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* IE10+ */
    background: linear-gradient(top,  #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 ); /* IE6-9 */
}

jQuery的

$("#selected-tab").addClass('someGradient');

之前

 <div id='selected-tab' class='arial'>Hello world</div>

 <div id='selected-tab' class='arial someGradient'>Hello world</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM