Switch the image size using replace string replacement in jQuery

Source: Internet
Author: User

The core is to replace the img src image path with replace to switch between images of different sizes.

The following animation shows the size switch. Click the "big image" button to change the src of the image, load the big image, and click the "small image" button to display the small image.

Replace usage:
I am actually a newbie, but I can't speak to Hai Han.
String. replace (a, B); represents using B to replace the part of the string containing a. For example, the string obj = "welcome to my website! "; Obj. replace (" my "," jb51net "); represents replacing my in the string obj with the string jb51net.

If you are interested, you can use the following code for testing:
Copy codeThe Code is as follows:
Var obj = "welcome to my website! ";
Var newobj = obj. replace ("my", "jb51net's ");
Alert (newobj );

Well, let's talk about this. More things can be learned on the instance page.

Core code:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Document). ready (function (){
// Convert the image height and width to the em Unit
$ (". Zxx_image"). each (function (){
Var emW = $ (this). width ()/128 + "em ";
Var emH = $ (this). height ()/128 + "em ";
((This).css ("width", emW );
Watermark (this).css ("height", emH)
});
Function srcChg (a, B ){
$ (". Zxx_image"). each (function (){
Var new_src = $ (this). attr ("src"). replace (a, B); // string replacement, change the image path
// Alert (new_src );
$ (This). attr ("src", new_src );
});
}
$ ("# Small_pic"). click (function (){
If ($ (this). hasClass ("on") return;
Else {
$ ("# Big_pic"). removeClass ("on ");
$ (This). addClass ("on ");
$ (". Zxx_image_list" ).css ("font-size", "128px ");
SrcChg ("s256", "s128 ");
Return false; // load IE6 slices after clicking
}
});
$ ("# Big_pic"). click (function (){
If ($ (this). hasClass ("on") return;
Else {
$ ("# Small_pic"). removeClass ("on ");
$ (This). addClass ("on ");
$ (". Zxx_image_list" ).css ("font-size", "256px ");
SrcChg ("s128", "s256 ");
Return false;
}
});
});
</Script>

Packaging jquery-replace-pic-convert.rar

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.