jQuery提取图像的主色升级版本图片轮播,主题色跟着变化

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

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

上次的分享不知道为什么用着轮播一轮就不好使了,于是上网查找等有很好的用法

 

 

需要插件:jquery.adaptive-backgrounds.js

其他jq、swper自行引入

JS

$(function(){
	//轮播	
	var mySwiper = new Swiper ('.swiper-container', {
	    loop: true,
	    autoplay : 3000,
		autoplayDisableOnInteraction : false,
	    // 如果需要分页器
	    pagination: '.swiper-pagination'
	});
	
	//获取图片颜色值
	function getcolor(){
		var img = document.getElementsByClassName('swiper-slide-active')[0].children[0];
		var colors = RGBaster.colors(img, {
		    paletteSize: 30,
		    success: function(colors){
		      img.style.borderColor = colors.palette[0];	
		      var makeDivWithClassAndBGColor = function(klass, color){
		        var div = document.createElement("div");
		        div.className = klass;
		        div.style.backgroundColor = color;
		        return div;
		      };
		      console.log("-----------------------------------------------");
		      console.log("Dominant color:", colors.dominant);
		      console.log("Secondary color:", colors.secondary);
		      console.log("Palette length:", colors.palette.length);
		      console.log("-----------------------------------------------");
		      //console.log("Dominant color:", colors.secondary);
		      var  bgcolor = colors.secondary;
		      $('.boxbg').css("background",bgcolor);
		    }
		});
	    
	};
	
	setint = setInterval(getcolor,10);
	getcolor();

});

HTML

<main class="container">
<!--轮播-->
	<div class="swiper-box">
		<div class="boxbg"></div>
			<div class="swiper-container">
			    <div class="swiper-wrapper">
			        <div class="swiper-slide"><img src="http://g.hiphotos.baidu.com/image/pic/item/5243fbf2b211931376d158d568380cd790238dc1.jpg"></div>
			        <div class="swiper-slide"><img src="http://a.hiphotos.baidu.com/image/pic/item/4a36acaf2edda3ccc4a53e450ce93901213f9216.jpg"></div>
			        <div class="swiper-slide"><img src="http://a.hiphotos.baidu.com/image/pic/item/35a85edf8db1cb1371b75377d054564e93584bf3.jpg"></div>
			        <div class="swiper-slide"><img src="http://h.hiphotos.baidu.com/image/pic/item/b999a9014c086e064a76b12f0f087bf40bd1cbfc.jpg"></div>
			    </div>
			    <!-- 如果需要分页器 -->
			    <div class="swiper-pagination"></div>
		</div>
	</div>		
</main>

CSS

<style type="text/css">
			.container .swiper-box {
			    width: 100%;
			    height: 200px;
			    background: #fff;
			    position: relative;
			}
			.container .swiper-box .boxbg {
			    width: 100%;
			    height: 179px;
			    position: relative;
			    border-bottom-left-radius: 10px;
			    border-bottom-right-radius: 10px;
			    background: #8BA9E7;
			}
			.swiper-container {
			    width: 100%;
			    height: auto;
			    position: absolute;
			    top: 0;
			}
			.swiper-container img {
			    width: 90%;
			    height: 197px !important;
			    border-radius: 10px;
			    display: block;
			    height: 200px;
			    margin: 40px auto;
			    /* padding-top: 3px; */
			    box-shadow: 0px 3px 20px 1px #ddd;
			}
</style>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WebCsDn_TDCode

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

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

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

打赏作者

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

抵扣说明:

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

余额充值