Javascript+CSS實現影像捲簾效果思路及代碼_javascript技巧

來源:互聯網
上載者:User

用過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> 
相關文章

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.