Javascript+CSS實現影像捲簾效果,javascriptcss
用過Arcgis的筒子們對於Arcmap裡面的一個捲簾效果肯定記憶很深刻,想把它搬到自己的WebGIS系統中去,抱著同樣的想法,我也對這種比較炫的捲簾效果做了一下研究,吼吼,出來了,給大家彙報一下成果,先看張圖:
捲簾效果
看到這樣的效果,你是不是小雞動了一下,嘿嘿,別急,聽我慢慢道來。
首先,容器。分別用兩個DIV來顯示兩個不同時期的影像。接下來設定兩個容器的樣式,代碼:
#after{ position: absolute; top: 0px; left: 0px; background-image: url(../images/24.jpg); width: 940px; height: 529px; background-repeat: no-repeat;}#before{ position: absolute; top: 0px; left: 0px; border-right: 3px solid #f00; background-image: url(../images/23.jpg); width: 433px; height: 529px; background-repeat: no-repeat; max-width: 940px;}這樣,圖片就在web上顯示出來了。
接下來實現捲簾效果。實現捲簾,最主要的是設定before的寬度,如何去設定呢,就是擷取滑鼠的位置,代碼如下:
function RollImage(evt){ var x=evt.pageX; console.log(x); $("#before").css("width",x+"px"); } </script>
這樣,捲簾的效果就實現了。原始碼如下:
style.css
.beforeafter{ width: 940px; height: 529px;}#after{ position: absolute; top: 0px; left: 0px; background-image: url(../images/24.jpg); width: 940px; height: 529px; background-repeat: no-repeat;}#before{ position: absolute; top: 0px; left: 0px; border-right: 3px solid #f00; background-image: url(../images/23.jpg); width: 433px; height: 529px; background-repeat: no-repeat; max-width: 940px;}
test.html
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"><head> <title>日本地震災區前後對比</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Language" content="zh-CN"> <link href="css/roll.css" type="text/css" rel="stylesheet"> <script src="../jquery-1.8.3.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> function RollImage(evt){ <strong>var x=evt.pageX; $("#before").css("width",x+"px");</strong> } </script></head><body><div class="beforeafter" onmousemove="RollImage(event)"> <div id="after"></div> <div id="before"> </div></div></body></html>
做一個帶箭頭的圖片切換的效果,只用javascript與css來實現
自已做的,功能有些不完善,多指教,代碼如下:
<style type="text/css">
#scroll{ width:140px; height:210px; overflow:hidden;border:1px solid red;}
#scroll ul{ width:700px; height:210px; padding:0; margin:0; list-style:none;}
#scroll ul li{ width:138px; height:208px; float:left; border:1px solid yellow;}
#scroll ul li img{width:138px; height:208px;}
.container{ padding:0 60px; width:140px; position:relative;}
#left,#right{ width:50px; height:50px; position:absolute; top:80px; border:1px solid blue; cursor:pointer;}
#left{left:0px;}
#right{right:0px;}
</style>
<div class="container">
<div id="left"></div>
<div id="scroll">
<ul>
<li><img src="0.jpg" alt="0.jpg"></li>
<li><img src="1.jpg" alt="1.jpg"></li>
<li><img src="2.jpg" alt="2.jpg"></li>
<li><img src="3.jpg" alt="3.jpg"></li>
<li><img src="4.jpg" alt="4.jpg"></li>
</ul>
</div>
<div id="right"></div>
</div>
<script type="text/javascript">
var endscroll,T;
function scrol(direction,endscroll,step){
var obj1=document.getElementById("scroll");
if(direction=="right&q......餘下全文>>
利用javascript動態選擇css外部樣式串連實現的原理
瀏覽器在解析HTML頁面時,會自動載入css串連檔案,引入外部css檔案有兩種,一種是使用html標籤的link,另外一種是在<style>標籤裡面用import函數(CSS專用的)
無論哪種,只要他們有變化,瀏覽器就會自動載入css樣式檔案到當前頁面並執行修改頁面內容。
使用javascript動態建立link節點到頁面就能實現這個效果,也就是你問的原理效果就出來了。