gpt4 book ai didi

jquery - 使用十六进制值和变量改变 jquery 中的 .css 渐变

转载 作者:行者123 更新时间:2023-11-28 09:21:43 26 4
gpt4 key购买 nike

我在我正在构建的网站主体中设置了背景渐变。我还创建了一个菜单,其元素在悬停时会变为不同的颜色。我想要做的是只要求背景渐变的部分更改为悬停在菜单项上的相同颜色。我已经能够使用 JQuery 使用预选的十六进制值来更改背景颜色。我想知道是否可以结合选择“this”或悬停元素颜色的变量来定义这些十六进制值。如果没有,如果有另一种可能更简单的方法来解决问题。

这是我的 HTML:

<div id="header">Jane K Callahan &#124; Freelancer Writer</div>
<hr>
<div>
<ul id="menu">
<li>Portfolio</li>
<li>Services</li>
<li>Testimonials</li>
<li>Blog</li>
<li>About Jane</li>
<li>Contact</li>

</ul>
</div>

</div>

我的相关 CSS:

     html {
height: 100%;
}

body {
max-width: 1100px;
min-width: 800px;
font-family: Rockwell;
font-size: 13px;
font-color: #3a3a3c;
width: 100%;
margin: 0;
height: 100%;
background: -webkit-linear-gradient(left bottom, #ffffff 20%, #f8f8f8, #f0f0f0, #e8e8e8, #e0e0e0, #d8d8d8, #d0d0d0, #e8e8e8, #ffffff 50%, #f8f8f8, #f0f0f0, #e8e8e8); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(top right #ffffff 20%, #f8f8f8, #f0f0f0, #e8e8e8, #e0e0e0, #d8d8d8, #d0d0d0, #e8e8e8, #ffffff 50%, #f8f8f8, #f0f0f0, #e8e8e8); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(top right #ffffff 20%, #f8f8f8, #f0f0f0, #e8e8e8, #e0e0e0, #d8d8d8, #d0d0d0, #e8e8e8, #ffffff 50%, #f8f8f8, #f0f0f0, #e8e8e8); /* For Firefox 3.6 to 15 */
background: linear-gradient(to top right #ffffff 20%, #f8f8f8, #f0f0f0, #e8e8e8, #e0e0e0, #d8d8d8, #d0d0d0, #e8e8e8, #ffffff 50%, #f8f8f8, #f0f0f0, #e8e8e8); /* Standard syntax */
background-repeat: no-repeat;
background-attachment: fixed;
}

#header {
font-family: Copperplate;
font-size: 25px;
font-color: #080808;
margin-top: 120px;
margin-left: -50%;
width: 45%;
min-width: 500px;
font-variant: small-caps;
position: fixed;
}


hr {
margin-top: 150px;
width: 45%;
min-width: 530px;
margin-left: -100px;
color: #505050;
background-color: #505050;
height: 2px;
border: grooved 2px;
box-shadow: 4px 4px 15px 0px rgba(50, 50, 50, 0.75);
position: fixed;

}

#menu {
margin-top: 200px;
margin-left: 90px;
float: left;
width: 15%;
min-width: 150px;
position: fixed;
}

ul {
list-style: none;
}

li {
font-weight: bold;
padding: 8px 8px 8px 20px;
border: 1px solid gray;
margin-bottom: 10px;
}

li a {
color: inherit;
}

#menu li:nth-child(1) {
background-color: #333333;
color: #ffffff;
}

#menu li:nth-child(1):hover {
background-color: #f16745;
-webkit-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
}

#menu li:nth-child(2) {
background-color: #5c5c5c;
color: #ffffff;
}

#menu li:nth-child(2):hover {
background-color: #ffc65d;
-webkit-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
}

#menu li:nth-child(3) {
background-color: #858585;
color: #ffffff;
}

#menu li:nth-child(3):hover {
background-color: #7bc8a4;
-webkit-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
}


#menu li:nth-child(4) {
background-color: #adadad;
color: #1a1a1a;
}

#menu li:nth-child(4):hover {
background-color: #4cc3d9;
-webkit-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
}

#menu li:nth-child(5) {
background-color: #d6d6d6;
color: #1a1a1a;
}

#menu li:nth-child(5):hover {
background-color: #93648d;
-webkit-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
}

#menu li:nth-child(6) {
background-color: #ffffff;
color: #1a1a1a;
}

#menu li:nth-child(6):hover {
background-color: #404040;
color: #ffffff;
-webkit-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
}

最后,我的 JQuery-in-progress:

$(function() {
$("li").hover(function() {
var color = $(this).css("background-color");
$("hr").css({"background-color": color});
$("body").css({"background": "-webkit-linear-gradient(left bottom, #ffffff 20%, #f8f8f8, #f0f0f0, " + color + "#e0e0e0, #d8d8d8, #d0d0d0, " + color + "#ffffff 50%, #f8f8f8, #f0f0f0, " + color + ")"} /* For Safari 5.1 to 6.0 */
//{"background:" "-o-radial-gradient(#ffffff 70%, #f8f8f8, #f7f7f7, #f0f0f0, #e8e8e8, #e0e0e0, #f16745"}, /* For Opera 11.1 to 12.0 */
//{"background:" "-moz-radial-gradient(#ffffff 70%, #f8f8f8, #f7f7f7, #f0f0f0, #e8e8e8, #e0e0e0, #f16745"}, /* For Firefox 3.6 to 15 */
//{"background:" "radial-gradient(#ffffff 70%, #f8f8f8, #f7f7f7, #f0f0f0, #e8e8e8, #e0e0e0, #f16745"} /* Standard syntax */
);
},
function() {
$("hr").css({"background-color": "#505050"});
$("body").css({"background": "-webkit-linear-gradient(left bottom, #ffffff 20%, #f8f8f8, #f0f0f0, #e8e8e8, #e0e0e0, #d8d8d8, #d0d0d0, #e8e8e8, #ffffff 50%, #f8f8f8, #f0f0f0, #e8e8e8)"});
}
);

});

例如,我尝试用预定义的“颜色”变量替换第一个渐变中的十六进制值“#e8e8e8”。

最佳答案

按下面的按钮查看实际效果。

你很接近。每行都是这样的:

$("body").css({"background": "-webkit-radial-gradient(#ffffff 70%, #f8f8f8, #f0f0f0, #e8e8e8, " + color + ")"}

$(function() {
$("li").hover(function() {
var color = $(this).css("background-color");
$("hr").css({
"background-color": color
});
$("body").css({
"background": "-webkit-linear-gradient(left bottom, #ffffff 20%, #f8f8f8, #f0f0f0, " + color + ",#e0e0e0, #d8d8d8, #d0d0d0, " + color + ",#ffffff 50%, #f8f8f8, #f0f0f0, " + color + ")"
} /* For Safari 5.1 to 6.0 */
//{"background:" "-o-radial-gradient(#ffffff 70%, #f8f8f8, #f7f7f7, #f0f0f0, #e8e8e8, #e0e0e0, #f16745"}, /* For Opera 11.1 to 12.0 */
//{"background:" "-moz-radial-gradient(#ffffff 70%, #f8f8f8, #f7f7f7, #f0f0f0, #e8e8e8, #e0e0e0, #f16745"}, /* For Firefox 3.6 to 15 */
//{"background:" "radial-gradient(#ffffff 70%, #f8f8f8, #f7f7f7, #f0f0f0, #e8e8e8, #e0e0e0, #f16745"} /* Standard syntax */
);
},
function() {
$("hr").css({
"background-color": "#505050"
});
$("body").css({
"background": "-webkit-linear-gradient(left bottom, #ffffff 20%, #f8f8f8, #f0f0f0, #e8e8e8, #e0e0e0, #d8d8d8, #d0d0d0, #e8e8e8, #ffffff 50%, #f8f8f8, #f0f0f0, #e8e8e8)"
});
}
);

});
html {
height: 100%;
}

body {
max-width: 1100px;
min-width: 800px;
font-family: Rockwell;
font-size: 13px;
font-color: #3a3a3c;
width: 95%;
margin: 0;
height: 100%;
background: -webkit-radial-gradient(#ffffff 70%, #f8f8f8, #f0f0f0, #e8e8e8, #e0e0e0, #d8d8d8, #d0d0d0); /* For Safari 5.1 to 6.0 */
background: -o-radial-gradient(#ffffff 70%, #f8f8f8, #f7f7f7, #f0f0f0, #e8e8e8, #e0e0e0, #d8d8d8, #d0d0d0); /* For Opera 11.1 to 12.0 */
background: -moz-radial-gradient(#ffffff 70%, #f8f8f8, #f7f7f7, #f0f0f0, #e8e8e8, #e0e0e0, #d8d8d8, #d0d0d0); /* For Firefox 3.6 to 15 */
background: radial-gradient(#ffffff 70%, #f8f8f8, #f7f7f7, #f0f0f0, #e8e8e8, #e0e0e0, #d8d8d8, #d0d0d0); /* Standard syntax */
background-repeat: no-repeat;
background-attachment: fixed;
}

#header {
font-family: Copperplate;
font-size: 25px;
font-color: #080808;
margin-top: 120px;
margin-left: -50%;
width: 45%;
min-width: 500px;
font-variant: small-caps;
position: fixed;
}


hr {
margin-top: 150px;
width: 45%;
min-width: 500px;
margin-left: -100px;
color: #505050;
background-color: #505050;
height: 2px;
border: grooved 2px;
box-shadow: 4px 4px 15px 0px rgba(50, 50, 50, 0.75);
position: fixed;

}

#menu {
margin-top: 200px;
margin-left: 90px;
float: left;
width: 15%;
min-width: 150px;
position: fixed;
}

li:nth-child(1) {
background-color: #333333;
color: #ffffff;
}

li:nth-child(1):hover {
background-color: #f16745;
-webkit-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
}

li:nth-child(2) {
background-color: #5c5c5c;
color: #ffffff;
}

li:nth-child(2):hover {
background-color: #ffc65d;
-webkit-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
}

li:nth-child(3) {
background-color: #858585;
color: #ffffff;
}

li:nth-child(3):hover {
background-color: #7bc8a4;
-webkit-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
}


li:nth-child(4) {
background-color: #adadad;
color: #1a1a1a;
}

li:nth-child(4):hover {
background-color: #4cc3d9;
-webkit-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
}

li:nth-child(5) {
background-color: #d6d6d6;
color: #1a1a1a;
}

li:nth-child(5):hover {
background-color: #93648d;
-webkit-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
}

li:nth-child(6) {
background-color: #ffffff;
color: #1a1a1a;
}

li:nth-child(6):hover {
background-color: #404040;
color: #ffffff;
-webkit-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
box-shadow: -9px 3px 15px 0px rgba(50, 50, 50, 0.75);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header">Jane K Callahan &#124; Freelancer Writer</div>
<hr>
<div>
<ul id="menu">
<li>Portfolio</li>
<li>Services</li>
<li>Testimonials</li>
<li>Blog</li>
<li>About Jane</li>
<li>Contact</li>

</ul>
</div>

</div>

更新:

我已经用您的新代码更新了上面的代码。您在变量后引用的位中缺少两个逗号,因为:

"background": "-webkit-linear-gradient(left bottom, #ffffff 20%, #f8f8f8, #f0f0f0, " + color + "#e0e0e0, #d8d8d8, #d0d0d0, " + color + "#ffffff 50%, #f8f8f8, #f0f0f0, " + color + ")"

应该是这样的:

"background": "-webkit-linear-gradient(left bottom, #ffffff 20%, #f8f8f8, #f0f0f0, " + color + ", #e0e0e0, #d8d8d8, #d0d0d0, " + color + ", #ffffff 50%, #f8f8f8, #f0f0f0, " + color + ")"

关于jquery - 使用十六进制值和变量改变 jquery 中的 .css 渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26070871/

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