jquery limits the size of a picture _jquery

Source: Internet
Author: User

The example in this article describes how jquery limits the size of a picture. Share to everyone for your reference, specific as follows:

Recently in an information site, the article content can display pictures, so you need to limit the user to put the picture displayed size.

Find a piece of code on the Web:

$ (document). Ready (function () {
  $ (#viewnews_body img). each (function () {
    var width = 620;
    var height =;
    var image = $ (this);
    if (Image.width () > Image.height ()) {
      if (image.width () >width) {
        image.width (width);
        Image.height (Width/image.width () *image.height ());
      }
    else{
      if (image.height () >height) {
        image.height (height);
        Image.width (Height/image.height () *image.width ());}}});


Use this method to realize the running effect is not stable, have the time picture has not finished loading will be executed first.

So instead of using a method that binds the load event to all pictures that need to be limited in size, this ensures that the code that restricts the size is executed separately after each picture is loaded. The code is as follows:

$ (document). Ready (function () {
  $ (#viewnews_body img). bind ("Load", function () {
    var width = 620;
    var height =;
    var image = $ (this);
    if (Image.width () > Image.height ()) {
      if (image.width () >width) {
        image.width (width);
        Image.height (Width/image.width () *image.height ());
      }
    else{
      if (image.height () >height) {
        image.height (height);
        Image.width (Height/image.height () *image.width ());}}});


More interested readers of jquery-related content can view the site's topics: A summary of Ajax usage in jquery, a summary of jquery table (table) operations tips, a summary of jquery drag-and-drop effects and techniques, a summary of jquery extension techniques, jquery Common Classic Effects Summary "jquery animation and special effects usage Summary", "jquery selector usage Summary" and "jquery common Plug-ins and Usage summary"

I hope this article will help you with the jquery program design.

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.