duanchan9354 2014-11-27 17:28
浏览 33

使用javascript自动更改图片[关闭]

I don't know whether this is the right place to ask the question.

Well I would like to use a script which automatically changes the picture of my start site of my website. So the script should check the month and if the month is march it uses picture1.jpg and if the month is august it uses picture2.jpg.

Is it possible to do that, e.g. using javascript?

I am looking forward to your answer(s) and would love it if you could help me creating such a script.

Thank you!

  • 写回答

1条回答 默认 最新

  • duankui1532 2014-11-27 17:35
    关注

    This is a sample and can be modified code:

    <img id="Logo" src="Images/default.png" alt="KnowledgeBase" width="75%" onload="logo(this)" />
    
    
        function logo(img) {
          if (img.src.indexOf('default')==-1) return; // already changed 
          var d = new Date();
          var Today = d.getDate();
          var Month = d.getMonth();
          var src;
          if (Month === 10 && (Today >= 23 && Today <= 26)) {
            src = "Images/doodles/blah1.png";
          } else if (Month === 11 && (Today >= 23 && Today <= 26)) {
            src = "Images/doodles/blah2.png";
          } else if ((Month === 11 && Today >= 30) || (Month === 0 && Today <= 2)) {
            src = "Images/doodles/blah3.png";
          } else if (Month === 6 && (Today >= 3 && Today <= 5)) {
            src = "Images/doodles/blah4.png";
          } 
          img.src=src;
        }
    

    DEMO

    Alternative:

    <script language="javascript" type="text/javascript"> 
    var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); 
    var thetime = new Date(); 
    var themonth = thetime.getMonth(); 
    document.write('<img src="image' + months[themonth] + '.gif" alt="Image of the month: ' + months[themonth] + '" />'); 
    </script>
    

    Alternative 3:

    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <div id="banner-container" style="width:400px;height:300px;"></div>
    
    <script>
    var currentTime = new Date();
    var month = currentTime.getMonth() + 1;
    var total = month;
    
    // Summer
    if (total >= 6 && total <= 8)
    {
        document.getElementById("banner-container").style.backgroundImage = "url('images/winter.png')";
    }
    // Autumn
    else if (total >= 9 && total <= 11)
    {
        document.getElementById("banner-container").style.backgroundImage="url('images/fall.png')";
    }
    // Winter
    else if (total == 12 || total == 1 || total == 2)
    {
        document.getElementById("banner-container").style.backgroundImage = "url('images/winter.png')";
    }
    // Spring
    else if (total >= 2 && total <= 6)
    {
        document.getElementById("banner-container").style.backgroundImage = "url('images/spring.png')";
    }
    else
    {
        document.getElementById("banner-container").style.backgroundImage = "url('images/summer.png')";
    }
    </script>
    
    </body> </html>
    

    More info: Changing image source based on date

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题