2011-11-16 91 views
2

所以我正在寫一個HTML文件,由iPad上的UIWebView在本地加載。頁面中的每個圖像都有一個「touchend」事件處理程序,它幾乎將圖像的src更改爲另一個圖像文件。當我這樣做時,由於某種原因,它會隨意更改其位置以及HTML樹中其他任何圖像元素的位置。這有什麼理由嗎?在iPad上從「touchend」事件更改圖像元素的「src」?

<html> 
    <head> 
    <title>Index</title> 
    </head> 
    <body> 
    <img id="backgroundImage" alt="" src="image445.jpg" /> 
    <img id="lockerImage" alt="" src="locker.png" /> 
    <img id="guyImage" alt="" src="guy.png" /> 
    <img id="capImage" alt="" src="cap.png" /> 
    <img id="maskImage" alt="" src="mask.png" /> 
    <img id="shieldImage" alt="" src="shield.png" /> 
    <img id="glovesImage" alt="" src="foldedGloves.png" /> 
    <img id="bootsImage" alt="" src="foldedBoots.png" /> 
    <img id="underGownImage" alt="" src="foldedUnderGown.png"/> 
    <img id="gownImage" alt="" src="gown.png" /> 
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript"> 
    var guyImage; 
    var capImage; 
    var maskImage; 
    var shieldImage; 
    var gownImage; 
    var underGownImage; 
    var glovesImage; 
    var bootsImage; 
    var originalCapImageLocationX = 580; 
    var originalCapImageLocationY = 80; 
    var originalMaskImageLocationX = 720; 
    var originalMaskImageLocationY = 80; 
    var originalShieldImageLocationX = 650; 
    var originalShieldImageLocationY = 70; 
    var originalGownImageLocationX = 750; 
    var originalGownImageLocationY = 300; 
    var originalUnderGownImageLocationX = 600; 
    var originalUnderGownImageLocationY = 200; 
    var originalGlovesImageLocationX = 560; 
    var originalGlovesImageLocationY = 400; 
    var originalBootsImageLocationX = 620; 
    var originalBootsImageLocationY = 400; 
    var originalGuyImageLocationX = 150; 
    var originalGuyImageLocationY = 0; 

    $(document).ready(function() { 
     //Position the background and locker in the right positions 
     var backgroundImage = $("#backgroundImage"); 
     backgroundImage.offset({ left:0, top:0}); 
     var lockerImage = $("#lockerImage"); 
     lockerImage.offset({left:$(document).width() - lockerImage.width(), top:0}); 
     //Prevent the webview from scrolling 
     document.body.addEventListener("touchmove", function (event) { 
      event.preventDefault(); 
     }, false); 
     //Get referneces of the images in html 
     guyImage = $("#guyImage"); 
     capImage = document.getElementById("capImage"); 
     maskImage = document.getElementById("maskImage"); 
     shieldImage = document.getElementById("shieldImage"); 
     gownImage = document.getElementById("gownImage"); 
     glovesImage = document.getElementById("glovesImage"); 
     bootsImage = document.getElementById("bootsImage"); 
     underGownImage = document.getElementById("underGownImage"); 

     //Add the drag event listeners to the images 
     capImage.addEventListener("touchmove", function (event) { 
      if (event.targetTouches.length == 1) { 
       var touch = event.targetTouches[0]; 
       moveImageCenterTo(touch.pageX, touch.pageY, $(capImage)); 
      } 
     }, false); 

     maskImage.addEventListener("touchmove", function (event) { 
      if (event.targetTouches.length == 1) { 
       var touch = event.targetTouches[0]; 
       moveImageCenterTo(touch.pageX, touch.pageY, $(maskImage)); 
      } 
     }, false); 

     shieldImage.addEventListener("touchmove", function (event) { 
      if (event.targetTouches.length == 1) { 
       var touch = event.targetTouches[0]; 
       moveImageCenterTo(touch.pageX, touch.pageY, $(shieldImage)); 
      } 
     }, false); 

     gownImage.addEventListener("touchmove", function (event) { 
      if (event.targetTouches.length == 1) { 
       var touch = event.targetTouches[0]; 
       moveImageCenterTo(touch.pageX, touch.pageY, $(gownImage)); 
      } 
     }, false); 

     underGownImage.addEventListener("touchmove", function (event) { 
      if (event.targetTouches.length == 1) { 
       var touch = event.targetTouches[0]; 
       moveImageCenterTo(touch.pageX, touch.pageY, $(underGownImage)); 
      } 
     }, false); 

     glovesImage.addEventListener("touchmove", function (event) { 
      if (event.targetTouches.length == 1) { 
       var touch = event.targetTouches[0]; 
       moveImageCenterTo(touch.pageX, touch.pageY, $(glovesImage)); 
      } 
     }, false); 

     bootsImage.addEventListener("touchmove", function (event) { 
      if (event.targetTouches.length == 1) { 
       var touch = event.targetTouches[0]; 
       moveImageCenterTo(touch.pageX, touch.pageY, $(bootsImage)); 
      } 
     }, false); 

     //Add the finger up events to the images 
     capImage.addEventListener("touchend", function (event) { 
      if (event.changedTouches.length == 1) { 
       var x = getCenterLocationX($(capImage)); 
       var y = getCenterLocationY($(capImage)); 
       if (isWithinGuysHead(x, y)) { 
        moveImageToLocation(getGuysHeadLeft() - 8, getGuysHeadTop(), $(capImage)); 
       } else { 
        moveImageCenterTo(originalCapImageLocationX, originalCapImageLocationY, $(capImage)); 
       } 

      } 
     }, false); 

     shieldImage.addEventListener("touchend", function (event) { 
      if (event.changedTouches.length == 1) { 
       var x = getCenterLocationX($(shieldImage)); 
       var y = getCenterLocationY($(shieldImage)); 
       if (isWithinGuysHead(x, y)) { 
        moveImageToLocation(getGuysHeadLeft() - 10, getGuysHeadTop() - 20 + (getGuysHeadBottom()/2), $(shieldImage)); 
       } else { 
        moveImageCenterTo(originalShieldImageLocationX, originalShieldImageLocationY, $(shieldImage)); 
       } 

      } 
     }, false); 

     maskImage.addEventListener("touchend", function (event) { 
      if (event.changedTouches.length == 1) { 
       var x = getCenterLocationX($(maskImage)); 
       var y = getCenterLocationY($(maskImage)); 
       if (isWithinGuysHead(x, y)) { 
        moveImageToLocation(getGuysHeadLeft() - 5, getGuysHeadTop() - 5 + (getGuysHeadBottom()/2), $(maskImage)); 
       } else { 
        moveImageCenterTo(originalMaskImageLocationX, originalMaskImageLocationY, $(maskImage)); 
       } 

      } 
     }, false); 

     gownImage.addEventListener("touchend", function (event) { 
      if (event.changedTouches.length == 1) { 
       var x = getCenterLocationX($(gownImage)); 
       var y = getCenterLocationY($(gownImage)); 
       if (isWithinGuysBody(x, y)) { 
        moveImageToLocation(getGuysXLocation() + 5, getGuysHeadBottom() - 8, $(gownImage)); 
       } else { 
        moveImageCenterTo(originalGownImageLocationX, originalGownImageLocationY, $(gownImage)); 
       } 

      } 
     }, false); 

     underGownImage.addEventListener("touchend", function (event) { 
      if (event.changedTouches.length == 1) { 
       var x = getCenterLocationX($(underGownImage)); 
       var y = getCenterLocationY($(underGownImage)); 
       if (isWithinGuysBody(x, y)) { 
        $(underGownImage).attr("src", "underGown.png"); 
        moveImageToLocation(getGuysXLocation() + 25, getGuysHeadBottom() - 8, $(underGownImage)); 

       } else { 
        $(underGownImage).attr("src","foldedUnderGown.png"); 
        moveImageCenterTo(originalUnderGownImageLocationX, originalUnderGownImageLocationY, $(underGownImage)); 
       } 

      } 
     }, false); 

     glovesImage.addEventListener("touchend", function (event) { 
      if (event.changedTouches.length == 1) { 
       var x = getCenterLocationX($(glovesImage)); 
       var y = getCenterLocationY($(glovesImage)); 
       if (isWithinAnyGuysHand(x, y)) { 
        $(glovesImage).attr("src","gloves.png"); 
        moveImageToLocation(getGuysXLocation(), getGuysHandsTopEdge() - 28, $(glovesImage)); 
       } else { 
        $(glovesImage).attr("src","foldedGloves.png"); 
        moveImageCenterTo(originalGlovesImageLocationX, originalGlovesImageLocationY, $(glovesImage)); 
       } 
      } 
     }, false); 

     bootsImage.addEventListener("touchend", function (event) { 
      if (event.changedTouches.length == 1) { 
       var x = getCenterLocationX($(bootsImage)); 
       var y = getCenterLocationY($(bootsImage)); 
       if (isWithinGuysFeet(x, y)) { 
            $(bootsImage).attr("src","boots.png"); 
        moveImageToLocation(getGuysFeetLeftEdge() + 5, getGuysFeetTopEdge() - 5, $(bootsImage)); 
       } else { 
            $(bootsImage).attr("src","foldedBoots.png"); 
        moveImageCenterTo(originalBootsImageLocationX, originalBootsImageLocationY, $(bootsImage)); 
       } 

      } 
     }, false); 

     //Move the images to their original locations 
     moveImageCenterTo(originalCapImageLocationX, originalCapImageLocationY, $(capImage)); 
     moveImageCenterTo(originalMaskImageLocationX, originalMaskImageLocationY, $(maskImage)); 
     moveImageCenterTo(originalShieldImageLocationX, originalShieldImageLocationY, $(shieldImage)); 
     moveImageCenterTo(originalGownImageLocationX, originalGownImageLocationY, $(gownImage)); 
     moveImageCenterTo(originalUnderGownImageLocationX, originalUnderGownImageLocationY, $(underGownImage)); 
     moveImageCenterTo(originalGlovesImageLocationX, originalGlovesImageLocationY, $(glovesImage)); 
     moveImageCenterTo(originalBootsImageLocationX, originalBootsImageLocationY, $(bootsImage)); 
     moveImageToLocation(originalGuyImageLocationX, originalGuyImageLocationY, $(guyImage)); 
    }); 
</script> 

我剛剛發佈的相關代碼。在此先感謝:-)

回答

1

所以這個問題結束了在CSS中。默認情況下,每個項目的位置是「靜態的」,所以我所做的只是將其更改爲「絕對」,以便在圖像的src發生變化並因此寬度和高度變化時,不會混淆其他位置元素。