简体   繁体   English

使用JavaScript刷新页面时如何更改图像?

[英]how to change the image when a page is refreshed using JavaScript?

I want to change the image when the page is refresh using html. 我想在使用html刷新页面时更改图像。 I place my partial code here. 我将我的部分代码放在这里。 i want a script or anything do change the image when the page gets refresh.. Please help me to do this using html ... 我希望脚本或其他任何东西都能在页面刷新时更改图像。.请帮助我使用html ...

        <div class="one-image">
           <a href="">
            <img src="img/IMG_1035.jpg" class="giThumbnail" alt="Nightclubs"></a><h4 class="giDescription">
             Nightclubs</h4>
             </div>

the above image tag image is change every refresh.. please help me .. 上面的图片标签图片每次刷新都会改变..请帮助我..

I believe this would work, but all your images would have to be sequentially named, eg 1-100. 我相信这会起作用,但是您所有的图像都必须顺序命名,例如1-100。 Also note that the script was placed AFTER the IMG tag. 另请注意,脚本已放置在IMG标签之后。

<div class="one-image">            
  <a href="">             
    <img id="imgRand" src="" class="giThumbnail" alt="Nightclubs">
  </a>
  <h4 class="giDescription">              
    Nightclubs
  </h4>              
</div> 

<script language="javascript">
  // random number between 1 and 100
  var numRand = Math.floor(Math.random()*101);
  document.getElementById("imgRand").src = "img/IMG_"+numRand+".jpg";
</script>

The random formula in JS is: JS中的随机公式为:

var random = Math.floor(Math.random() * (max - min + 1)) + min; 

so if you only had 5 images between 135 and 140 your script would look like: 因此,如果您只有5张介于135至140之间的图片,您的脚本将如下所示:

var random = Math.floor(Math.random() * (140 - 135 + 1)) + 135; 

If you wanted to change the image client-side, like a slideshow, just add a timer. 如果您想更改图像的客户端(如幻灯片),只需添加一个计时器。

<script language="javascript">
  function setImg(){
  // random number between 1 and 100
  var numRand = Math.floor(Math.random()*101);
  document.getElementById("imgRand").src = "img/IMG_"+numRand+".jpg";}


  // call it the first time
  setImg();

  // set an interval to change it every 30 seconds
  window.setInterval("setImg()",30000);
</script>

Not Tested but something like this should work using Javascript: 尚未测试,但是使用Javascript应该可以执行以下操作:


//Add following inside script tag
//Add id to your image tag
var theImages = new Array();
theImages[0] = 'images/first.gif' // replace with names of images
theImages[1] = 'images/second.gif' // replace with names of images
theImages[2] = 'images/third.gif' // replace with names of images
......
var j = 0
var p = theImages.length;
var preBuffer = new Array();

for (i = 0; i < p; i++){
    preBuffer[i] = new Image();
    preBuffer[i].src = theImages[i];
}
var whichImage = Math.round(Math.random()*(p-1));

function showImage(){
    document.getElementById("yourImgTagId").src = theImages[whichImage];
}

//call the function 
showImage();

Did you mean something like that 你的意思是那样的吗

What type of server are you using? 您使用哪种服务器? Using PHP or ASP you can accomplish this using a script that cycles through your images in a script. 使用PHP或ASP,您可以使用在脚本中循环浏览图像的脚本来完成此操作。 So your HTML would just point to the script instead of directly to an image: 因此,您的HTML只会指向脚本,而不是直接指向图像:

<img src="image_rotation_script.php" class="giThumbnail" alt="Nightclubs">

Then your script would rotate through your various images. 然后您的脚本将轮流浏览各种图像。 Here is a PHP example: 这是一个PHP示例:

<?php
header('content-type: image/jpeg');
// Your own logic here to pull from a database, randomize an array of images etc.
$images = array('img/IMG_1.jpg','img/IMG_2.jpg','img/IMG_3.jpg');
$imageFile = array_rand($images);
$image = imagecreatefromjpeg($imageFile);
imagejpeg($image);
imagedestroy($image);
?>

Use can use JavaScript to change the src field of the image tag. 使用可以使用JavaScript更改图片标签的src字段。 First insert an ID attribute to your img tag, or a NAME attribute if it appears more often on the same page. 首先在您的img标记中插入一个ID属性,如果在同一页面上更经常出现,则插入一个NAME属性。

<img id="nightClubImage" src="img/IMG_1035.jpg" class="giThumbnail" alt="Nightclubs">

Then write a script and change the src field: 然后编写脚本并更改src字段:

document.getElementById('nightClubImage').src = "newImage.jpg";

If you want use only client-side solution try this: 如果只想使用客户端解决方案,请尝试以下操作:

<img src="img/IMG_1035.jpg" class="giThumbnail" alt="Nightclubs">
<script type="text/javascript">
    var images = ['img/IMG_1035.jpg', 'img/IMG_1036.jpg', 'img/IMG_1037.jpg'];
    document.getElementById('gitThumbnail').src = images[Math.round((Math.random() * (images.length-1))];
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 html/javascript 中刷新页面时,如何更改图像的位置? - How can i change the position of an image when page is refreshed in html/javascript? 刷新页面时如何停止页面加载图像 - How to stop page loading image when page is refreshed 刷新页面后,使用javascript构建的计时器会重置吗? 如何避免呢? - Timer built using javascript gets reset when page is refreshed? How to avoid it? angularJS:页面刷新时更改图像。 否则(如果未刷新)要在5秒内自动更改 - angularJS: Change Image when Page Refresh. otherwise (if not refreshed) want to change automatically in 5 sec 刷新页面时使用图像更改 div,但响应式,在手机上使用特定图像,在桌面上使用另一个图像 - Change the div with image when refreshed the page, but responsive, with a specific image on the phone and another on the desktop 刷新页面后Javascript不起作用(Rails) - Javascript not working when page is refreshed (Rails) 刷新页面时,“提交”按钮不起作用-javascript - Submit button not working when the page is refreshed -javascript 使用JavaScript单击时如何更改图像? - How to change an image when clicked using JavaScript? Javascript间歇性失败,但刷新页面后即可使用 - Javascript fails intermittently but works when page is refreshed 刷新页面时重置计时器javascript - timer is reset when the page is refreshed javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM