简体   繁体   中英

Automatic image resizing if browser width =

I asked this question today and got some great answers (thanks to the guys who helped me out there :) ).

Now please have a look at the following code. I'm a 100% sure the resizing part works, but my if/else statement doens't work (I'm still a JS rookie). I also mentioned this in my previous topic, but someone said I should rather post a new question.

(The script should detect someones browser width, so it can resize the #fluidimage) note: the resizing part works. Only the viewportwidth detection and the if/else statement isnt functional yet.

$(window).load ( function () {

    function resizer (index, measurement) {
        var imageresize = 80;
        var viewportWidth = width();

       if ((viewportWidth >= 1680)) {
            imageresize = 100;
       } else if ((viewportWidth <= 1680) && (viewportWidth > 1280)) {
            imageresize = 80;
       } else if ((viewportWidth <= 1280) &&  (viewportWidth > 1024)) {
            imageresize = 60;
       } else if ((viewportWidth <= 1024) ) {
            imageresize = 40;
       } else {
            imageresize = 100;
       }


        this.wCall = (typeof this.wCall == "null") ? true : this.wCall ^ true;
        return this.wCall ? Math.round (measurement * imageresize / 100) : measurement;
    }
    $("#fluidimage").width (resizer).height (resizer);
} );

Change:

var viewportWidth = width();

To:

var viewportWidth = $(window).width();


See it at jsFiddle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM