JS+CSS實(shí)現(xiàn)的漂亮漸變背景特效代碼(6個(gè)漸變效果)
本文實(shí)例講述了JS+CSS實(shí)現(xiàn)的漂亮漸變背景特效代碼。分享給大家供大家參考,具體如下:
運(yùn)行效果截圖如下:

具體代碼如下:
<html>
<head>
<title>
JS配合CSS實(shí)現(xiàn)的漂亮漸變背景特效6個(gè)實(shí)例
</title>
<script>
var setGradient = (function() {
var p_dCanvas = document.createElement('canvas');
var p_useCanvas = !!(typeof(p_dCanvas.getContext) == 'function');
var p_dCtx = p_useCanvas ? p_dCanvas.getContext('2d') : null;
var p_isIE =
/*@cc_on!@*/
false;
try {
p_dCtx.canvas.toDataURL()
} catch(err) {
p_useCanvas = false;
};
if (p_useCanvas) {
return function(dEl, sColor1, sColor2, bRepeatY) {
if (typeof(dEl) == 'string') dEl = document.getElementById(dEl);
if (!dEl) return false;
var nW = dEl.offsetWidth;
var nH = dEl.offsetHeight;
p_dCanvas.width = nW;
p_dCanvas.height = nH;
var dGradient;
var sRepeat;
if (bRepeatY) {
dGradient = p_dCtx.createLinearGradient(0, 0, nW, 0);
sRepeat = 'repeat-y';
} else {
dGradient = p_dCtx.createLinearGradient(0, 0, 0, nH);
sRepeat = 'repeat-x';
}
dGradient.addColorStop(0, sColor1);
dGradient.addColorStop(1, sColor2);
p_dCtx.fillStyle = dGradient;
p_dCtx.fillRect(0, 0, nW, nH);
var sDataUrl = p_dCtx.canvas.toDataURL('image/png');
with(dEl.style) {
backgroundRepeat = sRepeat;
backgroundImage = 'url(' + sDataUrl + ')';
backgroundColor = sColor2;
};
}
} else if (p_isIE) {
p_dCanvas = p_useCanvas = p_dCtx = null;
return function(dEl, sColor1, sColor2, bRepeatY) {
if (typeof(dEl) == 'string') dEl = document.getElementById(dEl);
if (!dEl) return false;
dEl.style.zoom = 1;
var sF = dEl.currentStyle.filter;
dEl.style.filter += ' ' + ['progid:DXImageTransform.Microsoft.gradient( GradientType=', +( !! bRepeatY), ',enabled=true,startColorstr=', sColor1, ', endColorstr=', sColor2, ')'].join('');
};
} else {
p_dCanvas = p_useCanvas = p_dCtx = null;
return function(dEl, sColor1, sColor2) {
if (typeof(dEl) == 'string') dEl = document.getElementById(dEl);
if (!dEl) return false;
with(dEl.style) {
backgroundColor = sColor2;
};
}
}
})();
</script>
<style>
body{font:0.75em/1.4 Arial;text-align:left;margin:20px;} hr{clear:both;visibility:hidden;}
xmp{font:12px/12px "Courier New";background:#fff;color:#666; border:solid
1px #ccc;} div.example{ border:solid 1px #555;margin:0 20px 20px 0;float:left;
display:inline;margin:1em;background:#fff;width:300px;padding:40px;color:#222;font:xx-small/1.2
"Tahoma";text-align:justify;}
</style>
<body>
<div id="example1" class="example">
CSS特效代碼。
</div>
<div id="example2" class="example">
各類編程源碼、
</div>
<div id="example3" class="example">
精品軟件
</div>
<div id="example4" class="example">
上海世博園
</div>
<div id="example5" class="example">
我家北京天安門
</div>
<div id="example6" class="example">
北京歡迎您!
</div>
<script>
setGradient('example1', '#4ddbbe', '#d449cc', 1);
setGradient('example2', '#46ddbd', '#d8b617', 0);
setGradient('example3', '#c81fc1', '#bf445f', 1);
setGradient('example4', '#2285e5', '#d769eb', 0);
setGradient('example5', '#8b4286', '#eac87d', 1);
setGradient('example6', 'black', 'white', 0);
</script>
</body>
</html>
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
jQuery實(shí)現(xiàn)頁(yè)面頂部下拉廣告
本文主要分享了jQuery實(shí)現(xiàn)頁(yè)面頂部下拉廣告的具體代碼。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2016-12-12
原生js實(shí)現(xiàn)trigger方法示例代碼
這篇文章主要給大家介紹了關(guān)于利用原生js實(shí)現(xiàn)trigger方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用js具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
微信小程序圖片輪播組件gallery slider使用方法詳解
這篇文章主要為大家詳細(xì)介紹了微信小程序圖片輪播組件gallery slider的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Javascript動(dòng)態(tài)綁定事件的簡(jiǎn)單實(shí)現(xiàn)代碼
Javascript事件綁定的方法很多,很靈活。不過,作為比較簡(jiǎn)單的動(dòng)態(tài)綁定,下面的代碼看似正確,但得不到預(yù)期的效果。2010-12-12
window.open的頁(yè)面如何刷新(父頁(yè)面)上層頁(yè)面
在開發(fā)過程中會(huì)經(jīng)常遇到window.open打開的頁(yè)面,同時(shí)問題出現(xiàn)了如何刷新上層頁(yè)面呢?本文將詳細(xì)介紹,需要了解的朋友可以參考下2012-12-12
移動(dòng)端Ionic App 資訊上下循環(huán)滾動(dòng)的實(shí)現(xiàn)代碼(跑馬燈效果)
這篇文章主要介紹了移動(dòng)端Ionic App 資訊上下循環(huán)滾動(dòng)的實(shí)現(xiàn)代碼,實(shí)現(xiàn)方法需要借助jQuery庫(kù)的選擇器和動(dòng)畫函數(shù),并且把jquery的操作封裝到指令里,具體指令代碼大家通過本文學(xué)習(xí)吧2017-08-08
JS獲取子窗口中返回的數(shù)據(jù)實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狫S獲取子窗口中返回的數(shù)據(jù)實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05

