The concrete realization _jquery of the picture width and height with the jquery equal proportion control

Source: Internet
Author: User

Core code:

$ (function () { 
$ (". Dvcontent img"). each (function () { 
var maxwidth = 520; 
if ($ (this). Width () > maxwidth) { 
var oldwidth = $ (this). width (); 
var oldheight = $ (this). Height (); 
var newheight = maxwidth/oldwidth*oldheight; 
$ (this). css ({width:maxwidth+ "px", height:newheight+ "px", cursor: "pointer"}); 
$ (this). attr ("title", "Click to view original image"); 
$ (this). Click (function () {window.open ($ (this). attr ("src"))};}); 
 

If the above code does not execute, you can use the following code:

$ (window). Load (function () {
	$ (". Dvcontent img"). each (function () { 
	var maxwidth =; 
	if ($ (this). Width () > maxwidth) { 
	var oldwidth = $ (this). width (); 
	var oldheight = $ (this). Height (); 
	var newheight = maxwidth/oldwidth*oldheight; 
	$ (this). css ({width:maxwidth+ "px", height:newheight+ "px", cursor: "pointer"}); 
	$ (this). attr ("title", "Click to view original image"); 
	$ (this). Click (function () {window.open ($ (this). attr ("src"))};}); 

There is also a method of using CSS compatible IE6 can make the picture in excess of the specified width automatically proportional reduction, but the wording does not conform to the standard. The code is as follows:

. Cate img{
    max-width:600px; 
    Height:auto; 
    Width:expression (This.width > 600?) "600px": this.width);
 

So in order to be compatible with IE and other browsers and meet the standards of the international standard of the image through JS to control the width of the picture, the following use jquery to control the maximum width of the picture display, the main code is as follows:

$ (window). Load (function () {
    $ (". Cate img"). each (function () {
        var maxwidth =;
        if ($ (this). Width () > MaxWidth) {
            $ (this). Width (maxwidth);
        }
    });

The code is very simple, is the Cate style so the maximum width of IMG can only be 600px. each (function () {...}), where each calls the following method for the specified picture collection object. This jquery method enables you to control the maximum width of a picture display in IE6 and above browsers and Chrome and Firefox.

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.