JavaScript Layout transformation

Source: Internet
Author: User

The JavaScript layout transformation is to set the original floating layout through the JavaScript properties Offsetleft and offsettop default values in JS to give the element absolute positioning.

Original: Disadvantage-we want to add a drag-and-drop event to the image on the page we found that no location is impossible to complete all we have to use the following layout conversion

1<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">234<style>5* {margin:0; padding:0;}6 7 #ul1 {width:660px; position:relative; margin:10px Auto;}8#ul1 li {width:200px; height:150px;float: Left; list-style:none; margin:10px;}9</style>Ten<script> One</script> A<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/> -<title> Untitled Document </title> - the  -<body> -<ul id= "UL1" > -<li></li> +<li></li> -<li></li> +<li></li> A<li></li> at<li></li> -<li></li> -<li></li> -<li></li> -<li></li> -</ul> in</body> -

After conversion: After the conversion through JS to each Li gave absolute positioning

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">;} #ul1 {width:660px; position:relative; margin:10px Auto;} #ul1 li {width:200px; height:150px;float: Left; list-style:none; margin:10px;}</style><script>window.onload=function(){    varUl1=document.getelementbyid (' Ul1 '); varLis=ul1.getelementsbytagname (' Li '); varapos=[]; //get the Keft and top of each Li element saved to apos with a for loop     for(i=0;i<lis.length;i++) {Apos[i]={Left:lis[i].offsetleft,top:lis[i].offsettop}}//To add the last saved value to an element through a for loop     for(i=0;i<lis.length;i++) {Lis[i].style.left=apos[i].left+ ' px '; Lis[i].style.top=apos[i].top+ ' px '; Lis[i].style.position= ' absolute '; Lis[i].style.margin= ' 0 '; }}</script><meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/><title> Untitled document </title>

JavaScript Layout transformation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.