Use CSS and JavaScript to control the size of the page picture width

Source: Internet
Author: User

Jorwangの small Horn: "Recently busier Ah, temporarily do not update, we play a little bit Oh!" O (∩_∩) o~ "

Just see two control page picture width of the method, feel very good very convenient, hereby share to everyone.

The first approach is to use CSS to achieve control size:

img{
border:0;
margin:0;
padding:0;
max-width:590px;
Width:expression (this.width>590?) 590px ": this.width); max-height:590px;
Height:expression (this.height>590?) 590px ": this.height);
}

If you want to be compatible with IE6, you can use the JavaScript script described previously.

The second approach is to use JavaScript to control the size of the page picture width:

function set_img_size (obj, width) {if (!obj) return;
        if (!width) {width = obj.clientwidth * 0.9; if (width <) return; /* Set the picture's limited width */} Img_items = Obj.getelementsbytagname ("img") if (Img_items) {for (i=0; i<img_i Tems.length;
            i++) {var s_width = img_items[i].width;
            var s_height = img_items[i].height;
                if (s_width > width) {img_items[i].style.width = width + "px"; if (s_height = = img_items[i].height) {img_items[i].style.height = ((width/s_width) * img_items[i].h
                Eight) + "px";
                } Img_items[i].onclick = function () {window.open (THIS.SRC)};
                Img_items[i].style.cursor = "pointer";
            Img_items[i].alt = "Click to view original Size"; function Selfadaptation_size () {if (typeof (content_img_width) = = ' undefined ') set_img _size (DOCUMENT.GETELEMENTBYid ("posts")); /* Control the image of the element ID number */Else Set_img_size (document.getElementById ("posts"), content_img_width); /* Control the image of the element ID number/set_img_size (document.getElementById ("Custom"));
/* Control the image of the element ID number */} if (Window.addeventlistener) window.addeventlistener ("Load", selfadaptation_size, false);
else if (window.attachevent) window.attachevent ("onload", selfadaptation_size); else window.onload = selfadaptation_size;

If you have a better and more concise way, share it with me, thank you!

Related Article

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.