1

私の目標は、下部に 3 つのクリック可能なホット スポットがある画像スライド ショーを作成することです。これで基本的な機能はなくなりましたが、最初に画像をループするまで、コンテンツ div のサイズが適切に調整されません。ページがロードされたらすぐに、すべてのサイズと位置を正しく設定する必要があります。

<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">     
        $(document).ready(function () {
            $("#image").attr("src", images[0]);         
        });     

        //Click tracking
        var badClicks = 0;
        var skipClicks = 0;
        var goodClicks = 0;

        //Counter to keep track of images
        var iCount = 0;
        var images = [];
        images[0] = "images/document.png";
        images[1] = "images/document2.png";
        images[2] = "images/document3.png";

        function nextImage(){
            iCount++;

            //If there are no more images in the array, go back to the first image
            if (images[iCount]==null) {
                iCount = 0;
            };

            //Change image source to new image
            $("#image").attr("src", images[iCount]);

            //Set content wrapper width & height to current image's width & height
            $("#content").css("width", $("#image").css("width"));
            $("#content").css("height", $("#image").css("height"));

            //Store content wrapper's new width and height into variables
            var h = parseInt($("#content").css("height"));
            var w = parseInt($("#content").css("width"));

            //Move hotspot-wrapper to the bottom of the new image
                //Height of content wrapper - height of hotspot wrapper = new top
            $("#hotspot-wrapper").css("top", h - parseInt($("#hotspot-wrapper").css("height")));


            console.log(images[iCount] + " h " + h + " w " + w);
        }

        //Do something with data for "bad" hotspot
        function bad(){
            badClicks++;
        }

        //Do something with data for "skip" hotspot
        function skip(){
            skipClicks++;
            nextImage();
        }

        //Do something with data for "good" hotspot
        function good(){
            goodClicks++;
        }

        //Show the collected data
        function displayResults(){
            $("#results").append("<br />Bad: " + badClicks 
            + " Skip: " + skipClicks 
            + " Good: " + goodClicks);
        }
    </script>
  </head>
  <body>
    <div id="content">      
        <img id="image" />
        <div id="hotspot-wrapper">
            <div id="hotspot-a" class="hotspot" onclick="bad();"></div>
            <div id="hotspot-b" class="hotspot" onclick="skip();"></div>
            <div id="hotspot-c" class="hotspot" onclick="good();"></div>
        </div>
    </div>
        <br />
        <div id="results" style="clear:both">
            <button onclick="displayResults();" style="text-align: center">Show results</button>
        </div>

ヘルプ、ヒント、アドバイスをいただければ幸いです。ありがとう!

4

2 に答える 2

3

まず、画像配列を初期化するには、この方法を好みます。

var images = [];
var image = new Image();
image.src = '/some/image/url.jpg';
/* while you doing this image already load in background - so its faster way*/
images.push(image);

この画像を jQuery で次のように表示できます。

$('#parend_of_image_div').html(images[iCount]);

そして、nextImage関数内で次のコードを使用します。

var img = images[iCount];
$(img).load(function(){
    var width = $(this).width()
    var height = $(this).height();
    /* do other stuff */
});
于 2012-10-09T19:00:06.003 に答える
2

これはあなたの問題です

 $("#image").css("width")

ただし、css で #image の幅を設定していません。使用する必要があります

$('#image').width()

また、安全のために、画像が読み込みイベントをトリガーした後にのみ、コードのこの部分を続行する必要があります。

//Change image source to new image
$("#image").attr("src", images[iCount]).load(function(){

    //Set content wrapper width & height to current image's width & height
    $("#content").css("width", $("#image").width());
    $("#content").css("height", $("#image").height());

    //And then the rest...
于 2012-10-09T18:53:20.540 に答える