最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to change between 3 images with javascript onClick event? - Stack Overflow

programmeradmin2浏览0评论

I have simple html and Javascript code shown below where image1 is displayed, and when it is clicked it changes to image2. I am having a problem extending this to a third image such that if image2 is clicked, it changes to image3 and finally if image3 is clicked that it returns to image1 and the cycle can be repeated once more. Can anyone suggest to me what the best approach is because I think that the way I am currently doing it for 2 images won't work for what I have described. Thanks

Note:This is a problem I have encountered for exam preparation and as such I can't use JQuery-must be JavaScript.

<html> 
<head> 
<script> 
function preloadImages() { 
Image1=new Image(); 
Image1.src = "image1.jpg";
} 

</script> 
</head> 
<body onLoad="preloadImages();"> 
<img name="myButton" src="image2.jpg"
onClick="document.myButton.src='image1.jpg';" 
onClick="document.myButton.src='image2.jpg';"> 
</body> 
</html> 

I have simple html and Javascript code shown below where image1 is displayed, and when it is clicked it changes to image2. I am having a problem extending this to a third image such that if image2 is clicked, it changes to image3 and finally if image3 is clicked that it returns to image1 and the cycle can be repeated once more. Can anyone suggest to me what the best approach is because I think that the way I am currently doing it for 2 images won't work for what I have described. Thanks

Note:This is a problem I have encountered for exam preparation and as such I can't use JQuery-must be JavaScript.

<html> 
<head> 
<script> 
function preloadImages() { 
Image1=new Image(); 
Image1.src = "image1.jpg";
} 

</script> 
</head> 
<body onLoad="preloadImages();"> 
<img name="myButton" src="image2.jpg"
onClick="document.myButton.src='image1.jpg';" 
onClick="document.myButton.src='image2.jpg';"> 
</body> 
</html> 
Share Improve this question edited Apr 19, 2014 at 11:42 kellzer asked Apr 19, 2014 at 11:33 kellzerkellzer 1311 gold badge3 silver badges19 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

How about event listeners and an array of images that can be extended to any number you like

<html>

    <head></head>

    <body>
        <img id="myButton" src="image2.jpg" />
        <script type="text/javascript">
            var images = ['image1.jpg', 'image2.jpg', 'image3.jpg'],
                i = 1;

            // preload
            for (var j=images.length; j--;) {
                var img = new Image();
                img.src = images[j];
            }

            // event handler
            document.getElementById('myButton').addEventListener('click', function() {
                this.src = images[i >= images.length - 1 ? i = 0 : ++i];
            }, false);
        </script>
    </body>

</html>

FIDDLE

<html> 
<head> 
<script> 

var imagesNames = ["Image1.jpg", "Image2.jpg", "Image3.jpg"];

function preloadImages() { 
  for (var i = 0 ; i < imagesNames.length; i++) {
    var img = new Image();
    img.src = imageNames[i];
  }
} 

var imageIndex = 0;
img.onclick = function() {
  imageIndex++;
  if (imageIndex > imagesNames.length) {
    imageIndex = 0;
  }

  document.getElementById("myButton").src = imageNames[imageIndex];
}

</script> 
</head> 
<body onLoad="preloadImages();"> 
<img id="myButton">
</body> 
</html> 

you can toggle between three classes and attach different images to all classes to see how to toggle between three classes check this question here. You can also use an array and assign src of images to the array and assign one to your img at a time.Hope it helps

var mylinks = new Array();
mylinks[0] = "abc.jpg";
mylinks[1] = "xyz.jpg";
mylinks[2] = "lmn.jpg";

first assign one say (var i=0) and you assign mylinks[i] and then (i=i+1) and assign mylinks[i] and keep on doing it till i> mylinks.length then start-over i.e i=0;

HTML :

<img name="myButton" id="myButton" data-img="1" src="image1.jpg" />

JS:

var imgList = ["image1.jpg","image2.jpg", "image3.jpg" ];

var myButton = document.getElementById("myButton");
myButton.onclick = function( e ){
    var elem = e.target,
    imageIndex = parseInt(elem.dataset.img,10);
    if( imageIndex <= (imgList.length -1) ) {
        elem.src = imgList[imageIndex++];
        elem.dataset.img = imageIndex;
    } else {
        elem.src = imgList[0];
        elem.dataset.img = 1;
    }
}

Fiddle : http://jsfiddle/aslancods/ry6hS/

发布评论

评论列表(0)

  1. 暂无评论