javascript - 使用 css 属性反向线性渐变

标签 javascript jquery html css

我有一个充满线性渐变颜色的 div。单击按钮我想反转线性渐变颜色。我通过改变线性渐变值再次实现了它。我想知道的不是更改线性渐变值,而是有任何特定的 CSS 属性来反转线性渐变。希望你理解我的问题

var color  =  document.getElementById('color');
var btn = document.getElementById('reverse');
reverse.onclick = reverseColor;
function reverseColor(){
 color.style.background = "linear-gradient(to right, rgb(237,195 ,194) 0% ,rgb(237,195 ,194) 20%, rgb(226,167 ,165) 20% ,rgb(226,167 ,165) 40%, rgb(215,138 ,136) 40% ,rgb(215,138 ,136) 60%, rgb(203,110 ,106) 60% ,rgb(203,110 ,106) 80%, rgb(192,81 ,77) 80% ,rgb(192,81 ,77) 100%)";
}
.linear{
  width:200px;
  height:30px;
  background: linear-gradient(to right, rgb(192,81 ,77) 0% ,rgb(192,81 ,77) 20%, rgb(203,110 ,106) 20% ,rgb(203,110 ,106) 40%, rgb(215,138 ,136) 40% ,rgb(215,138 ,136) 60%, rgb(226,167 ,165) 60% ,rgb(226,167 ,165) 80%, rgb(237,195 ,194) 80% ,rgb(237,195 ,194) 100%);
}
<div id="color" class="linear">

</div>
<button id="reverse" style="margin-top:50px">
Reverse Gradient
</button>

最佳答案

您可以在线性渐变中切换向右向左

JSFIDDLE

代码如下:

var color = document.getElementById('color');
var btn = document.getElementById('reverse');
myVar = "left";

function colorfn() {
  color.style.background = "linear-gradient(to " + myVar + ", rgb(237,195 ,194) 0% ,rgb(237,195 ,194) 20%, rgb(226,167 ,165) 20% ,rgb(226,167 ,165) 40%, rgb(215,138 ,136) 40% ,rgb(215,138 ,136) 60%, rgb(203,110 ,106) 60% ,rgb(203,110 ,106) 80%, rgb(192,81 ,77) 80% ,rgb(192,81 ,77) 100%)";
}
btn.onclick = function() {
  myVar = myVar == "left" ? "right" : "left";
  colorfn();
}

colorfn();
.linear {
  width: 200px;
  height: 30px;
}
<div id="color" class="linear">

</div>

<button id="reverse" style="margin-top:50px">
  Reverse Gradient
</button>

关于javascript - 使用 css 属性反向线性渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42688833/

相关文章:

javascript - 无法使用 window.location 重定向到页面

javascript - ConstantSourceNode 设置的振荡器频率错误

jquery - 中心跨越 Bootstrap

jquery - 链接 :visited not work

javascript - 如何取消选中多个选定的选择器中的选项

html - 为什么我的图像在 Edge 中因纯 CSS 视差滚动而中断?

JavaScript/jQuery 可以做一些改进

javascript - 如何使用 wp_localize_script 从另一个 php 文件传递​​参数

jquery - 如何将今天的日期与 Django 使用 JavaScript/JQuery 提供的日期进行比较?

javascript - 想在打开另一个 div 时关闭一个 div