【问题标题】:Any ideas of how I can make my previous and next buttons on either side of image?关于如何在图像两侧制作上一个和下一个按钮的任何想法?
【发布时间】:2016-04-11 01:50:09
【问题描述】:

如何在图像的任一侧制作上一个和下一个按钮?这是我的代码:

<head>
  <title>change picture</title>
  <script type="text/javascript">
    function displayNextImage() {
      x = (x === images.length - 1) ? 0 : x + 1;
      document.getElementById("img").src = images[x];
    }

    function displayPreviousImage() {
      x = (x <= 0) ? images.length - 1 : x - 1;
      document.getElementById("img").src = images[x];
    }

    function startTimer() {
      setInterval(displayNextImage, 5000);
    }

    var images = [],
      x = -1;


<script type="text/javascript">
<!--
 var imlocation = "images/";
 var currentdate = 0;
 var image_number = 0;
 function ImageArray (n) {
   this.length = n;
   for (var i =1; i <= n; i++) {
     this[i] = ' '
   }
 }
 image = new ImageArray(3)



      
    images[0] = "";
    images[1] = "";
    images[2] = "";
    images[3] = "";
    
 var rand = 60/image.length
 function randomimage() {
    currentdate = new Date()
    image_number = currentdate.getSeconds()
    image_number = Math.floor(image_number/rand)
    return(image[image_number])
 }
 document.write("<img src='" + imlocation + randomimage()+ "'>");
//-->
</script>
  
  </script>
</head>

<body onload="startTimer()">
  <center><div id="img-box">
    <img id="img" src="" />
    </div></center>
    
    
    <button class="button button400" onclick="displayPreviousImage()">Previous</button>

              
       <button class="button button400"onclick="displayNextImage()">Next</button>
</body>

【问题讨论】:

  • 删除了绒毛,但问题仍然值得结束。

标签: javascript html image button


【解决方案1】:

你需要像这样重新排序一些元素:

<body onload="startTimer()">
         <button class="box button button400" onclick="displayPreviousImage()">Previous</button>

         <div class="box" id="img-box">
           <img width="200" height="100" id="img" src="" />
         </div>

         <button class="box button button400" onclick="displayNextImage()">Next</button>
    </body>

并添加 CSS 脚本:

.box {
  display: inline-block;
  vertical-align: middle;
}

JSFiddle : Here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 2019-11-07
    相关资源
    最近更新 更多