Often, when we browse various web pages, there will be various animation effects displayed. As shown in the picture below, it is a commonly used product display method in many online malls. Similar products are displayed side by side on the window. If the user sees If you want to see the details of a product you have won, just put the mouse on the area of the product, the originally folded product will automatically expand, and the details will be displayed in front of the user, and this animation uses DOM+JS It is achieved through combination, and today’s small exercise is to achieve this effect.
First, implement the basic framework of the page in HTML, and encapsulate the four pictures in a div block named "container"
<!doctype html> <meta charset="UTF-8"> <html> <head> <title> 鼠标滑过页面自动变大 </title> <link rel="stylesheet" href="styles/reset.css" /> <link rel="stylesheet" href="styles/slidingdoors.css" /> <script src="slidlingdoors.js"></script> </head> <body> <div id="container"> <img src="./images/door1.png"/> <img src="./images/door2.png"/> <img src="./images/door3.png"/> <img src="./images/door4.png"/> </div> </body> </html>
Then, I used two style sheets to modify the general style as follows:
followed by
slidingdoors.css和reset.css: #container { height: 477px; margin: 0 auto; border-right: 1px solid #ccc; border-bottom: 1px solid #ccc; overflow: hidden; position: relative; } #container img { position: absolute; display: block; left: 0; border-left: 1px solid #ccc; }
/** * Eric Meyer's Reset CSS v2.0 (http://meyerweb.com/eric/tools/css/reset/) * http://cssreset.com */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } table { border-collapse: collapse; border-spacing: 0; }
Next, we need to implement the sliding effect. The js code is as follows:
window.onload=function(){ var box=document.getElementById("container"); var imgs=box.getElementsByTagName("img"); var imgwidth=imgs[0].offsetWidth; var exposewidth=160; var boxwidth=imgwidth+exposewidth*(imgs.length-1); box.style.width=boxwidth+'px'; function setImgPos(){ for(var i=1;i<imgs.length;i++) { imgs[i].style.left=imgwidth+exposewidth*(i-1)+'px'; } } setImgPos(); var translate=imgwidth-exposewidth; for(var i=0;i<imgs.length;i++) { (function(i){ imgs[i].onmouseover=function(){ setImgPos(); for(var j=1;j<=i;j++) { imgs[j].style.left=parseInt(imgs[j].style.left,10)-translate+'px'; } }; })(i); } };
The finished rendering is as follows:
Problems encountered:
1. The i and j variables in the picture reset function are unclear;
2. The result is that after one picture is reset, the rest of the unreset pictures are blocked. This is mainly because there is a small problem with the reset function.
Experience: JS function variables are complicated and the logic is rigorous. You must be careful when writing code
The above is the entire content of this article, I hope it will be helpful to everyone’s study.