【问题标题】:Apply gradient or solid color on dynamic element在动态元素上应用渐变或纯色
【发布时间】:2015-08-25 18:35:28
【问题描述】:

我正在尝试使用 jquery 将渐变或纯色应用于元素。为什么使用 jQuery?因为这个网站有几种不同的颜色组合,所以生成 css 类的工作量太大了。

假设我有一个从 JSON 调用生成的列表

<div class="colours">
 <ul>
  <li class="black white"></li>
  <li class="green"></li>
 </ul>
</div>

如您所见,我需要一个带有渐变(黑色/白色)的列表元素和一个纯色(绿色)元素。

所以我的问题是如何在列表类中有两种颜色时设置渐变,或者在只需要一种颜色时设置纯​​色??

在我下面的代码中,当我尝试应用颜色时,它总是给我“productHtml 不是函数”或[object object]

$.each(data.product.custom, function(i, custom) {

        var productsHtml = [];
        $.each(custom.values, function(index, value){
          var color = (value.title).toLowerCase();

          var colorClean = color.replace(/\s?\/\s?/," ");
          var colors = colorClean.split(/\s+/);
          if (colors.length===1) {
            colors[1] = colors[0];
          }

       // var productHtml = '' +
       //        '<li class="'+colorClean+'" ></li>';

         var productHtml = $('<li></li>').css({ 
            'background': '-moz-linear-gradient(-45deg,  + colors[0] + 0%, + colors[0] + 49%, + colors[1] + 49%, + colors[1] + 100%)',
            //etc etc
        });
          productsHtml.push(productHtml);
        });
        productsHtml = productsHtml.join('');

        $('.product.'+id+' .colours').html('<ul>'+productsHtml+'</ul>');

      });

我做错了什么?

【问题讨论】:

  • 我冒险猜测您的字符串 ('-moz-linear-gradient(-45deg, + colors[0] + 0%, + colors[0] + 49%, + colors[1] + 49%, + colors[1] + 100%)') 是问题所在;您需要将变量与字符串连接起来(给出:'-moz-linear-gradient(-45deg,' + colors[0] + ' 0%', + colors[0] + '49%,' + colors[1] + '49%', + colors[1] + '100%)'
  • 你为什么在background这一行的最后使用,
  • 连字符内的变量不会被 Javascript 计算。使用字符串连接+
  • 这看起来比编辑、扩展或重置 css 要痛苦得多。

标签: javascript jquery css


【解决方案1】:

这是使用数据属性的解决方案。

  1. 首先,我遍历 .colours 中的所有
  2. 元素。
  3. 然后找到它的data-colour属性。
  4. 使用必要的线性梯度信息创建一个字符串。
  5. 在其数据属性中添加所有颜色(用“,”分隔)。
  6. 关闭线性梯度信息字符串。
  7. 将线性梯度应用于具有css() 的css 规则。

注意它可以有任何颜色值:

  • data-colour("rgb(255,123,43)");
  • data-colour("#222 #546");
  • data-colour("rgb(2,150,255) #3a1");

$(".colours").find('li').each(function(index, e) {
  var $elem = $(e);
  var colourattri = $(this).data("colour");
  var colours = colourattri.split(",");

  if (colours.length >= 2) {
    var linear = "linear-gradient(90deg, ";

    for (var index in colours) {
      linear += colours[index];
      if (index != colours.length - 1) {
        linear += ", ";
      }
    }
    linear += ")";
    $elem.css({
      background: linear,
    });
  } else if (colours.length == 1) {
    $elem.css("background-color", colours[0]);
  }
});
li {
  padding: 20px;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="colours">
  <ul>
    <li data-colour="black">Some text here</li>
    <li data-colour="black, white">Some text here</li>
    <li data-colour="red, blue">Some text here</li>
    <li data-colour="pink, white">Some text here</li>
    <!-- This can take a lot of colours-->
    <li data-colour="red, Orange, green, blue, indigo, violet">Some text here</li>
    <li data-colour="rgb(22,150,255), red, #2c3">Some text here</li>
    <li data-colour="rgb(22,150,255) 50%, red 60%, #2c3 90%">Some text here</li>
  </ul>
</div>

【讨论】:

  • 好的,这正是我想要的!只有一件事......是否可以设置例如 50% 白色和 50% 黑色?所以你会得到和普通的css规则一样的linear-gradient(135deg, #000000 0%,#000000 49%,#ffffff 49%,#ffffff 100%);
  • 你试过了吗?哦,我想我会为你更新它
  • 是的代码可以完美运行,唯一的问题是它需要是每种颜色的 50%,而不是真正的渐变;)
  • 现在它在“,”而不是“”(空格)上分割。
  • 好吧,我明白你做了什么(我认为);)......你从类名中获取的颜色是例如“黑色”等......我之前的问题实际上是如何添加 50 % 到该类名,使其显示为linear-gradient(135deg, #000000 0%,#000000 49%,#ffffff 49%,#ffffff 100%);??我自己无法添加,因为类名是动态生成的......
【解决方案2】:

我建议单独阅读每种颜色,然后应用渐变/纯色逻辑。喜欢:

$('li').each(function(){
    var classNames = $(this).attr("class").toString().split(' ');
    $.each(classNames, function (i, className) {
        // do solid color or gradient
    });
});

【讨论】:

  • 我不太明白...类名是动态生成的??!或者这是您在 json 调用之后执行的“功能”...您能再解释一下吗?
  • 不,它只是循环遍历每个列表项中的类名。
猜你喜欢
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 2016-12-02
  • 2013-03-22
  • 1970-01-01
相关资源
最近更新 更多