jquery cookie外掛程式輕鬆實現切換背景顏色

來源:互聯網
上載者:User

標籤:jquery cookie外掛程式 切換背景顏色

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>更換背景顏色</title>

<style type="text/css">

#bg{width: 90%;height: 500px;background: rgba(0,0,0,.2);position: fixed;bottom: 10px;left: 0;right: 0;margin:auto;display: none;border: 5px solid #fff;}

.close{width: 30px;height: 30px;border: 2ps solid;line-height: 30px;text-align: center;color: #fff;background: #000;position: absolute;right: 0;top: 0;font-size: 25px;}

.color{width: 200px;height: 100px;border: 1px solid #fff;float: left;margin:20px 35px;line-height: 100px;text-align: center;color: #fff;font-size: 30px;}

</style>

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

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

<script type="text/javascript">

$(function(){

var bgcolor = $.cookie(‘bgcolor‘);  //從cookie中讀取背景顏色

if(bgcolor){

document.body.style.backgroundColor = bgcolor; //顯示cookie中已經存在的顏色

$(‘#bg‘).css(‘backgroundColor‘,‘rgba(255,255,255,.7)‘);  //將id為bg的div背景色改為半透明的白色

}

$(‘#btn‘).on(‘click‘,function(){

$(‘#bg‘).show();

});

$(‘.close‘).on(‘click‘,function(){

$(‘#bg‘).hide();

});

//擷取class為color的元素,並設定背景色

$(‘.color‘).each(function(index,element){

var bgcolor = $(element).text();

$(this).css(‘backgroundColor‘,bgcolor);

$(element).on(‘click‘,function(){

document.body.style.backgroundColor = bgcolor;

$.cookie(‘bgcolor‘,bgcolor,{‘expires‘:30,‘path‘:‘/‘});

});

});

});

</script>

</head>

<body>

<button id="btn">切換皮膚</button>

<div id="bg">

<span class="color">green</span>

<span class="color">red</span>

<span class="color">yellow</span>

<span class="color">blue</span>

<span class="color">lightgreen</span>

<span class="color">pink</span>

<span class="color">orange</span>

<span class="color">gold</span>

<span class="color">purple</span>

<strong class="close">×</strong>

</div>

</body>

</html>


本文出自 “11996687” 部落格,請務必保留此出處http://12006687.blog.51cto.com/11996687/1862133

jquery cookie外掛程式輕鬆實現切換背景顏色

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.