[removed] random image selected on refresh

前端 未结 3 668
情话喂你
情话喂你 2020-12-30 13:30

So I have a site in which I have a description area and I have it be a random description on refresh by using the following code:



        
相关标签:
3条回答
  • 2020-12-30 14:19

    How about:

    HTML:

    <img id="image" />
    

    JS:

    var description = [
      "http://static.ddmcdn.com/gif/lightning-gallery-17.jpg",
      "http://static.ddmcdn.com/gif/lightning-gallery-18.jpg",
      "http://static.ddmcdn.com/gif/lightning-gallery-19.jpg",
      "http://static.ddmcdn.com/gif/lightning-gallery-20.jpg",
      "http://static.ddmcdn.com/gif/lightning-gallery-21.jpg"
    ];
    
    var size = description.length
    var x = Math.floor(size*Math.random())
    document.getElementById('image').src=description[x];
    

    No jQuery necessary.

    0 讨论(0)
  • 2020-12-30 14:25

    http://jsfiddle.net/mohammadAdil/SvswX/

    <img id='random'/>
    

    script -

    var image = new Array ();
    image[0] = "http://placehold.it/20";
    image[1] = "http://placehold.it/30";
    image[2] = "http://placehold.it/40";
    image[3] = "http://placehold.it/50";
    var size = image.length
    var x = Math.floor(size*Math.random())
    
    $('#random').attr('src',image[x]);
    
    0 讨论(0)
  • 2020-12-30 14:33

    You can have links with images with this code :
    At first insert this into your page :

    <a id='LinksRef'>
    <img id='BannersSrc'>
    </a>
    

    Then

    var Banners= [
      "http://static.ddmcdn.com/gif/lightning-gallery-17.jpg",
      "http://static.ddmcdn.com/gif/lightning-gallery-18.jpg",
      "http://static.ddmcdn.com/gif/lightning-gallery-19.jpg",
      "http://static.ddmcdn.com/gif/lightning-gallery-20.jpg",
      "http://static.ddmcdn.com/gif/lightning-gallery-21.jpg"
    ];
    
    var Links= [
      "http://static.ddmcdn.com/gif/lightning-gallery-17.jpg",
      "http://static.ddmcdn.com/gif/lightning-gallery-18.jpg",
      "http://static.ddmcdn.com/gif/lightning-gallery-19.jpg",
      "http://static.ddmcdn.com/gif/lightning-gallery-20.jpg",
      "http://static.ddmcdn.com/gif/lightning-gallery-21.jpg"
    ];
    var size = Banners.length
    var x = Math.floor(size*Math.random())
    document.getElementById('BannersSrc').src=Banners[x];
    document.getElementById('LinksRef').href=Links[x];
    
    0 讨论(0)
提交回复
热议问题