简体   繁体   中英

Changing Image on Orientation Change

So Im designing this application for iPad using phoneGap/Cordova and Jquery mobile.

after researching how i could change an image on orientation change and browsing through the epic resources that is Stack Overflow. I implemented this code.

// The event for orientation change
var onChanged = function () {

    // The orientation
    var orientation = window.orientation,
        // If landscape, then use "land" otherwise use "port"
        image = orientation == 90 || orientation == -90 ? "landscape" : "portrait";

    // Insert the image
    $('#introimg').html('<img src="images/appimages/' + image + '-intro-page.png">');

};

// Bind the orientation change event and bind onLoad
$(window).bind(orientationEvent, onChanged).bind('load', onChanged);

I am trying to change the image for an intro page. The html i Use is:

<div data-role="page" id="intro">

        <div data-role="content" class="mycontent" data-theme="a">
            <div id="introimg">
                <img src="images/appimages/portrait-intro-page.png" alt="" width="768" height="1004" id="imglayer1" />
                <br />
                <p id="imglayer2"><a href="#help" data-role="button" data-inline="true" data-theme="a">Start designing</a></p>
            </div>

        </div>  
    </div>

Can anyone say what im doing wrong, its probably something stupid but i can't see it. Any help would be greatly appreciated.

You can use classes for this:

var onChanged = function () {

    // The orientation
   var orientation = (window.orientation == -90 || window.orientation == 90) ? "landscape" : "portrait";

   // Insert the image class
  $("introimg").removeClass("portrait").removeClass("landscape").addClass(orientation);

};

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