Javascript应用--任你摆布的图片展示效果(图片名换成你自己的)

本文介绍了一种基于HTML、CSS和JavaScript实现的互动式图片缩放效果。当鼠标悬停在图片上时,图片会放大并跟随鼠标移动,同时显示图片描述。此效果通过动态调整图片的位置和大小来实现,提供了一种吸引用户的视觉体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Code:
  1. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <title>3</title>
  5. <styletype="text/css">
  6. html{
  7. overflow:hidden;
  8. }
  9. body{
  10. margin:0px;
  11. padding:0px;
  12. background:#000;
  13. position:absolute;
  14. width:100%;
  15. height:100%;
  16. cursor:crosshair;
  17. }
  18. #diapoContainer{
  19. position:absolute;
  20. left:10%;
  21. top:10%;
  22. width:80%;
  23. height:80%;
  24. background:#222;
  25. overflow:hidden;
  26. }
  27. .imgDC{
  28. position:absolute;
  29. cursor:pointer;
  30. border:#000solid2px;
  31. filter:alpha(opacity=90);
  32. opacity:0.9;
  33. visibility:hidden;
  34. }
  35. .spaDC{
  36. position:absolute;
  37. filter:alpha(opacity=20);
  38. opacity:0.2;
  39. background:#000;
  40. visibility:hidden;
  41. }
  42. .imgsrc{
  43. position:absolute;
  44. width:120px;
  45. height:67px;
  46. visibility:hidden;
  47. margin:4%;
  48. }
  49. #bkgcaption{
  50. position:absolute;
  51. bottom:0px;
  52. left:0px;
  53. width:100%;
  54. height:6%;
  55. background:#1a1a1a;
  56. }
  57. #caption{
  58. position:absolute;
  59. font-family:arial,helvetica,verdana,sans-serif;
  60. white-space:nowrap;
  61. color:#fff;
  62. bottom:0px;
  63. width:100%;
  64. left:-10000px;
  65. text-align:center;
  66. }
  67. </style>
  68. <scripttype="text/javascript">
  69. varxm;
  70. varym;
  71. /*====onmousemoveevent====*/
  72. document.onmousemove=function(e){
  73. if(window.event)e=window.event;
  74. xm=(e.x||e.clientX);
  75. ym=(e.y||e.clientY);
  76. }
  77. /*====windowresize====*/
  78. functionresize(){
  79. if(diapo)diapo.resize();
  80. }
  81. onresize=resize;
  82. /*====opacity====*/
  83. setOpacity=function(o,alpha){
  84. if(o.filters)o.filters.alpha.opacity=alpha*100;elseo.style.opacity=alpha;
  85. }
  86. ////////////////////////////////////////////////////////////////////////////////////////////
  87. /*=====encapsulatescript====*/
  88. diapo={
  89. O:[],
  90. DC:0,
  91. img:0,
  92. txt:0,
  93. N:0,
  94. xm:0,
  95. ym:0,
  96. nx:0,
  97. ny:0,
  98. nw:0,
  99. nh:0,
  100. rs:0,
  101. rsB:0,
  102. zo:0,
  103. tx_pos:0,
  104. tx_var:0,
  105. tx_target:0,
  106. ///////scriptparameters////////
  107. attraction:2,
  108. acceleration:.9,
  109. dampening:.1,
  110. zoomOver:2,
  111. zoomClick:6,
  112. transparency:.8,
  113. font_size:18,
  114. //////////////////////////////////
  115. /*====diaporesize====*/
  116. resize:function(){
  117. with(this){
  118. nx=DC.offsetLeft;
  119. ny=DC.offsetTop;
  120. nw=DC.offsetWidth;
  121. nh=DC.offsetHeight;
  122. txt.style.fontSize=Math.round(nh/font_size)+"px";
  123. if(Math.abs(rs-rsB)<100)for(vari=0;i<N;i++)O[i].resize();
  124. rsrsB=rs;
  125. }
  126. },
  127. /*====creatediapo====*/
  128. CDiapo:function(o){
  129. /*====initvariables====*/
  130. this.o=o;
  131. thisthis.x_pos=this.y_pos=0;
  132. thisthis.x_origin=this.y_origin=0;
  133. thisthis.x_var=this.y_var=0;
  134. thisthis.x_target=this.y_target=0;
  135. thisthis.w_pos=this.h_pos=0;
  136. thisthis.w_origin=this.h_origin=0;
  137. thisthis.w_var=this.h_var=0;
  138. thisthis.w_target=this.h_target=0;
  139. this.over=false;
  140. this.click=false;
  141. /*====createshadow====*/
  142. this.spa=document.createElement("span");
  143. this.spa.className="spaDC";
  144. diapo.DC.appendChild(this.spa);
  145. /*====createthumbnailimage====*/
  146. this.img=document.createElement("img");
  147. this.img.className="imgDC";
  148. this.img.src=o.src;
  149. thisthis.img.O=this;
  150. diapo.DC.appendChild(this.img);
  151. setOpacity(this.img,diapo.transparency);
  152. /*====mouseevents====*/
  153. this.img.onselectstart=newFunction("returnfalse;");
  154. this.img.ondrag=newFunction("returnfalse;");
  155. this.img.onmouseover=function(){
  156. diapo.tx_target=0;
  157. diapo.txt.innerHTML=this.O.o.alt;
  158. this.O.over=true;
  159. setOpacity(this,this.O.click?diapo.transparency:1);
  160. }
  161. this.img.onmouseout=function(){
  162. diapo.tx_target=-diapo.nw;
  163. this.O.over=false;
  164. setOpacity(this,diapo.transparency);
  165. }
  166. this.img.onclick=function(){
  167. if(!this.O.click){
  168. if(diapo.zo&&diapo.zo!=this)diapo.zo.onclick();
  169. this.O.click=true;
  170. this.O.x_origin=(diapo.nw-(this.O.w_origin*diapo.zoomClick))/2;
  171. this.O.y_origin=(diapo.nh-(this.O.h_origin*diapo.zoomClick))/2;
  172. diapo.zo=this;
  173. setOpacity(this,diapo.transparency);
  174. }else{
  175. this.O.click=false;
  176. this.O.over=false;
  177. this.O.resize();
  178. diapo.zo=0;
  179. }
  180. }
  181. /*====rearrangethumbnailsbasedon"imgsrc"imagesposition====*/
  182. this.resize=function(){
  183. with(this){
  184. x_origin=o.offsetLeft;
  185. y_origin=o.offsetTop;
  186. w_origin=o.offsetWidth;
  187. h_origin=o.offsetHeight;
  188. }
  189. }
  190. /*====animationfunction====*/
  191. this.position=function(){
  192. with(this){
  193. /*====settargetposition====*/
  194. w_target=w_origin;
  195. h_target=h_origin;
  196. if(over){
  197. /*====mouseover====*/
  198. w_target=w_origin*diapo.zoomOver;
  199. h_target=h_origin*diapo.zoomOver;
  200. x_target=diapo.xm-w_pos/2-(diapo.xm-(x_origin+w_pos/2))/(diapo.attraction*(click?10:1));
  201. y_target=diapo.ym-h_pos/2-(diapo.ym-(y_origin+h_pos/2))/(diapo.attraction*(click?10:1));
  202. }else{
  203. /*====mouseout====*/
  204. x_target=x_origin;
  205. y_target=y_origin;
  206. }
  207. if(click){
  208. /*====clicked====*/
  209. w_target=w_origin*diapo.zoomClick;
  210. h_target=h_origin*diapo.zoomClick;
  211. }
  212. /*====magicspringequations====*/
  213. x_pos+=x_varx_var=x_var*diapo.acceleration+(x_target-x_pos)*diapo.dampening;
  214. y_pos+=y_vary_var=y_var*diapo.acceleration+(y_target-y_pos)*diapo.dampening;
  215. w_pos+=w_varw_var=w_var*(diapo.acceleration*.5)+(w_target-w_pos)*(diapo.dampening*.5);
  216. h_pos+=h_varh_var=h_var*(diapo.acceleration*.5)+(h_target-h_pos)*(diapo.dampening*.5);
  217. diapo.rs+=(Math.abs(x_var)+Math.abs(y_var));
  218. /*====htmlanimation====*/
  219. with(img.style){
  220. left=Math.round(x_pos)+"px";
  221. top=Math.round(y_pos)+"px";
  222. width=Math.round(Math.max(0,w_pos))+"px";
  223. height=Math.round(Math.max(0,h_pos))+"px";
  224. zIndex=Math.round(w_pos);
  225. }
  226. with(spa.style){
  227. left=Math.round(x_pos+w_pos*.1)+"px";
  228. top=Math.round(y_pos+h_pos*.1)+"px";
  229. width=Math.round(Math.max(0,w_pos*1.1))+"px";
  230. height=Math.round(Math.max(0,h_pos*1.1))+"px";
  231. zIndex=Math.round(w_pos);
  232. }
  233. }
  234. }
  235. },
  236. /*====mainloop====*/
  237. run:function(){
  238. diapo.xm=xm-diapo.nx;
  239. diapo.ym=ym-diapo.ny;
  240. /*====captionanim====*/
  241. diapo.tx_pos+=diapodiapo.tx_var=diapo.tx_var*.9+(diapo.tx_target-diapo.tx_pos)*.02;
  242. diapo.txt.style.left=Math.round(diapo.tx_pos)+"px";
  243. /*====imagesanim====*/
  244. for(variindiapo.O)diapo.O[i].position();
  245. /*====loop====*/
  246. setTimeout("diapo.run();",16);
  247. },
  248. /*====loadimages====*/
  249. images_load:function(){
  250. //=====loopuntilallimagesareloaded=====
  251. varM=0;
  252. for(vari=0;i<diapo.N;i++){
  253. if(diapo.img[i].complete){
  254. diapo.img[i].style.position="relative";
  255. diapo.O[i].img.style.visibility="visible";
  256. diapo.O[i].spa.style.visibility="visible";
  257. M++;
  258. }
  259. resize();
  260. }
  261. if(M<diapo.N)setTimeout("diapo.images_load();",128);
  262. },
  263. /*====initscript====*/
  264. init:function(){
  265. diapo.DC=document.getElementById("diapoContainer");
  266. diapodiapo.img=diapo.DC.getElementsByTagName("img");
  267. diapo.txt=document.getElementById("caption");
  268. diapodiapo.N=diapo.img.length;
  269. for(i=0;i<diapo.N;i++)diapo.O.push(newdiapo.CDiapo(diapo.img[i]));
  270. diapo.resize();
  271. diapo.tx_pos=-diapo.nw;
  272. diapo.tx_target=-diapo.nw;
  273. diapo.images_load();
  274. diapo.run();
  275. }
  276. }
  277. </script>
  278. </head>
  279. <body>
  280. <divid="diapoContainer">
  281. <imgclass="imgsrc"src="conspiracy_21.jpg"alt="ReconsideryourExistence">
  282. <imgclass="imgsrc"src="conspiracy_22.jpg"alt="SomethingNeedstobeDiscovered">
  283. <imgclass="imgsrc"src="conspiracy_24.jpg"alt="TheySaidVeryLittle">
  284. <imgclass="imgsrc"src="conspiracy_26.jpg"alt="OnlyinYourMind">
  285. <imgclass="imgsrc"src="conspiracy_32.jpg"alt="ThePowerofImagination">
  286. <imgclass="imgsrc"src="conspiracy_29.jpg"alt="ObjectivityisImpossible">
  287. <imgclass="imgsrc"src="conspiracy_31.jpg"alt="CleaningUpOperation">
  288. <imgclass="imgsrc"src="conspiracy_17.jpg"alt="ArbitraryContents">
  289. <divid="bkgcaption"></div>
  290. <divid="caption"></div>
  291. </div>
  292. <scripttype="text/javascript">
  293. /*====startscript====*/
  294. functiondom_onload(){
  295. if(document.getElementById("diapoContainer"))diapo.init();elsesetTimeout("dom_onload();",128);
  296. }
  297. dom_onload();
  298. </script>
  299. </body>
  300. </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值