javascript - Broaden the scope of functions -


I have written some jQuery functions to change the size of the pictures, keeping their aspect ratio on pageload and changing window sizes. These works work great, but I made them specially for a scenario. Now, another similar scenario has arisen in which I should do almost the same functionality, only with IFrame In the interest of becoming a better coder, and not adding additional code, I can use this code for the iFrames of the site, as well as $ ('com-background & gt; img') briefly and efficiently. Am I Scenario?

Here's the jQuery:

  function imageCalc () {$ imgWidth = $ ('com-badge & gt; IMG') width (); $ ImgHeight = $ ('com-badge & gt; IMG'). Height (); $ ImgAspectRatio = $ imgHeight / $ imgWidth; $ ('Com-badge & gt; IMG'). CSS ('margin-left', '-10px'); } Function setImageDims () {$ ('Com-badge & gt; IMG') CSS ('height', function (calshhite) {return $ IMA spacreito * $ ('# main-content'). Width ();}); $ ('Com-badge & gt; IMG'). CSS ('width', function (calvdath) {return $ ('# main-content'). Width () + 20;}); } Function imageResize () {$ ('com-badge & gt; IMG'). CSS ('width', function (resize with) {return $ ("body"). Width ()}); $ ('Com-badge & gt; IMG'). CSS ('Height', Function (Resize Heat) {Return $ IMGSpecretitio * $ ('Com-badge & gt; IMG'). Width ();}); }    

What is @nnnnnn from comments:

Update the function to take the selector as an argument rather than hard-coding?

Turn it on:

  function imageCalc () {$ imgWidth = $ ('com-badge & gt; IMG'). Width (); $ ImgHeight = $ ('com-badge & gt; IMG'). Height (); $ ImgAspectRatio = $ imgHeight / $ imgWidth; $ ('Com-badge & gt; IMG'). CSS ('margin-left', '-10px'); }   

To do this:

  // Where the selector is your string query function, Choice comparison (selector) {$ imgWidth = $ (selector). Wide (); $ imgHeight = $ (selector). height (); $ ImgAspectRatio = $ imgHeight / $ imgWidth; $ (Selector) CSS ('margin-left', '-10px'); }   

The above changes will make your work normal , in the sense that you can do this with other DOM objects as long as you specify that How to change the Selector argument to do the same thing you are currently doing, call imageCalc ('com-badge & gt; IMG'); , and so on.

And this,

In addition to this, do not relate to your question, cache your jacose objects, do not re-select the same elements as the same function

That means:

  // Where the selector is your string query function image cache (selector) {var obj = $ (selector); $ ImgWidth = obj.width (); $ ImgHeight = obj.height (); $ ImgAspectRatio = $ imgHeight / $ imgWidth; Obj.css ('margin-left', '-10px'); }   

This is your cache your object, which means that you can find it once, and you can reuse the same object. This makes a difference in performance, because every time you use the $ (selector) , you are searching the DOM tree again to find that element, which takes time .

Comments

Post a Comment

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -