当然,想到这里,当不需要祝贺词的节日只需要将对应的祝贺词隐藏就行了(因为本菜鸟不是专注做UI的,所以这里着实花了几个小时的时间),不过总算有了预期的成果,好了,拿出来和有类似需求的童鞋们分享一下。
<!--advertisement code in html body--> <body> <!-- for advertisement note: take the right pic path below and in ad.js path total four place--> <div id="oImg" style="position: absolute; z-index: 100; top: 0; left: 0; width: 180px; height: 70px;"> <a href="http://gh.swpu.edu.cn" target="_blank"> <img src="festival/3.jpg" id=‘imgId‘ style=‘display:none‘/> </a> </div> <script type="text/javascript" src="festival/ad.js"></script> <!--end for advertisement --> </body>
// out link javascript code in ad.js window.onload = function() { var img = document.getElementById(‘imgId‘); var src = img.getAttribute(‘src‘); // care the right file path : festival/ var month = (new Date().getMonth()+1).toString(); //alert(month); if(month == ‘1‘ ||month == ‘3‘ || month == ‘5‘ || month == ‘9‘||month == ‘10‘ ) { img.setAttribute(‘src‘,‘festival/‘+month+‘.jpg‘); img.style.display = "block"; } else { var div = document.getElementById(‘oImg‘); img.style.display = "none"; div.style.display = "none"; //隐藏而不删除 } }; var floatImg = document.getElementById("oImg"); var delay = 10; //控制每次执行间隔的时间,做越大移动得越慢; var speed = 1; //控制每次执行移动的距离,值越大移动得越快; var flagX = 0; var flagY = 0; function toPixel(str1) { //该函数用于去掉数值后面的px,并将之转化为数字。 var oldLen = str1.length; var newLen = oldLen - 2; str2 = str1.slice(0, newLen); str3 = parseInt(str2); return str3; } function flowImg() { var bWidth = document.body.clientWidth; var bHeight = document.body.clientHeight; var bLeft = document.body.scrollLeft; var bTop = document.body.scrollTop; var iWidth = floatImg.offsetWidth; var iHeight = floatImg.offsetHeight; var iLeft = toPixel(floatImg.style.left); var iTop = toPixel(floatImg.style.top); //下面一段控制横向移动 if(iLeft < (bWidth - iWidth) && flagX == 0) { floatImg.style.left = (iLeft + speed) + "px"; } else if(iLeft >= (bWidth - iWidth) && flagX ==0) { flagX = 1; } else if(iLeft > 0 && flagX == 1) { floatImg.style.left = (iLeft - speed) + "px"; } else if(0 >= iLeft && flagX == 1) { flagX = 0; } //下面一段控制纵向移动 if(iTop < (bHeight - iHeight) && flagY == 0) { floatImg.style.top = (iTop + speed) + "px"; } else if(iTop >= (bHeight - iHeight) && flagY ==0) { flagY = 1; } else if(iTop > 0 && flagY == 1) { floatImg.style.top = (iTop - speed) + "px"; } else if(0 >= iTop && flagY == 1) { flagY = 0; } } var imgInterval = setInterval("flowImg()", delay); floatImg.onmouseover = function() {clearInterval(imgInterval);} floatImg.onmouseout = function() {imgInterval = setInterval("flowImg()", delay);}
注意:我的图片和ad.js文件都是在和html 文件同级festival 文件夹下
下面呈上我用到的几张素材祝贺词图片,如果有需要的直接下载下来改下名字就行了(最后一张图片无法显示,我把原版的javascript 文件 改后缀后上传了,要下载的自便):
一款兼容性好,自动变化的javascript 祝贺词代码(根据不同的节日自动显示不同的祝贺词),布布扣,bubuko.com