javascript - 通过单击目标循环渐变方向
问题描述
我需要通过单击一个目标 来循环所有八个渐变方向,如下所示:
$('.targ').on('click', function() {
let a = $(this).css('background').split(',')[0];
console.log(a); // I need `to top` here
if (a == 'to top') {
a = 'to top right'
} else if (a == 'to top right') {
a = 'to right'
} else if (a == 'to right') {
a = 'to right bottom'
}
// and so on
$(this).css('background', 'linear-gradient' + new_value);
});
.targ {
width: 54px;
height: 54px;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='targ' style='background: linear-gradient(to top,red,yellow)'></div>
有什么帮助吗?
解决方案
除了背景,您可以获取样式属性。
$(this).attr('style')
它会返回"background: linear-gradient(to top,red,yellow)"
。
然后你.slice(28)
用来切出"background: linear-gradient("
并.split(',')[0]
获得方向。
let a = $(this).attr('style').slice(28).split(',')[0];
推荐阅读
- javascript - 解码一个base45字符串,这将导致一个cbor压缩文件
- javascript - 如何在 django 的 for 循环中控制模型字段?
- javascript - 我通过 Flask 的 Chart.js 图形输出未显示所需的输出
- automation - 无论如何要自动查找未使用的 JavaScript 和 CSS?
- python - 迭代 tf.data.Dataset 的有效方法
- struct - Rust - 结构可以有一个可变成员吗?
- django - Django中重复的应用程序命名问题而不重命名应用程序
- ios - Obj-C - 在 NSObject 中定义 UIColor 属性?
- spring-boot - 使用 Spring Boot 使用 OpenID 提供程序对 Web 应用程序进行身份验证时出错
- reactjs - 使用 react useEffect 获取问题