jQuery提取图像的主色

图片主题色在图片所占比例较大的页面中,能够配合图片起到很好视觉效果,给人一种和谐、一致的感觉。同时也可用在图像分类,搜索识别等方面。通常主题色的提取都是在后端完成的,前端将需要处理的图片以链接或id的形式提供给后端,后端通过运行相应的算法来提取出主题色后,再返回相应的结果。

但是过程麻烦所以今天分享个插件直接获取

需要插件:

1.jquery.js v1.10.2(高版本会报错)

2.jquery.adaptive-backgrounds.js

3.waypoints.min.js

JS


$(document).ready(function(){
    // Make some selections.
    var $window       = $(window);
    var $imgWrapper   = $('.image-wrapper');
    var $imgs         = $imgWrapper.find("img");
    var $logoBoxes    = $('.logo .box');
    var $title        = $('h1');

    $imgs.on('ab-color-found', function(e, data){
    	console.log(data.color)
      $('.swatch').text(data.color)
    });

    // Run the A.B. plugin.
    $.adaptiveBackground.run({ parent: '1' });

    $imgWrapper.waypoint(function(direction) {

      if ( $(this).attr('data-colored') )
        return;

      $(this).css({ background: $(this).attr('data-color') })
             .attr('data-colored', 1);

    }, { 
      offset: '65%'
    })

})

HTML

<!DOCTYPE html>

<html>

<head>

  <title>Demo</title>

<link rel="stylesheet" href="styles.css" />

  <script src="jquery.js"></script>

  <script type="text/javascript" src='jquery.adaptive-backgrounds.js'></script>

  <script type="text/javascript" src='waypoints.min.js'></script>

   <script type="text/javascript" src='main.js'></script>
 
  

</head>

<body>


  <div class='image-wrapper slow'>

  <div class='inner'>

    <span class='color'>

      <span class='swatch'></span>

    </span>

    <img src="images/1.jpg" data-adaptive-background='1' data-description='grandpa'>

  </div>

</div>




</body>

</html>

效果

完整代码:https://download.csdn.net/download/qq_42396791/10744505

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WebCsDn_TDCode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值