JavaScript 网页背景图片随机化
2009-03-16 09:30
501 查看
单调的网站首页背景看多了不免有写厌烦,如果用户自己每刷新一次页面背景可以更换也许能为这种单调添一丝的新奇。这只需用一个JavaScript方法就可以搞定,通过随机函数在我们的一组图片中随机的选一张作为背景。
下面是JavaScript代码:
完整代码:(asp.net C#)
body#home.variation1 #wrapper { background-image: url(./img/tignish/home/feature-background-1.jpg); }
body#home.variation2 #wrapper { background-image: url(./img/tignish/home/feature-background-2.jpg); }
body#home.variation3 #wrapper { background-image: url(./img/tignish/home/feature-background-3.jpg); }
图片路径要换成你项目中的图片路径。
下面是JavaScript代码:
<mce:script type="text/javascript"><!-- // Add a class to the body tag to alternate background features var class_options = new Array( "variation1", "variation2", "variation3" ); if (Math.random) { var choice = Math.floor(Math.random() * (class_options.length)); // Just in case javascript gets carried away... choice = ( (choice < class_options.length) && choice >= 0) ? choice : 0; if (document.body.className == '') { document.body.className = class_options[choice]; } else { document.body.className += ' '+class_options[choice]; } } // --></mce:script>
完整代码:(asp.net C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>随机背景图片</title> <mce:style type="text/css"><!-- body#home { background-image: url(./img/tignish/home/background-tile.jpg); } body#home { background-position: top center; } #wrapper { min-width: 950px; height:500px; background-position: top center; background-repeat: no-repeat; margin:0; padding:0; } body#home.variation1 #wrapper { background-image: url(./img/tignish/home/feature-background-1.jpg); } body#home.variation2 #wrapper { background-image: url(./img/tignish/home/feature-background-2.jpg); } body#home.variation3 #wrapper { background-image: url(./img/tignish/home/feature-background-3.jpg); } --></mce:style><style type="text/css" mce_bogus="1"> body#home { background-image: url(./img/tignish/home/background-tile.jpg); } body#home { background-position: top center; } #wrapper { min-width: 950px; height:500px; background-position: top center; background-repeat: no-repeat; margin:0; padding:0; } body#home.variation1 #wrapper { background-image: url(./img/tignish/home/feature-background-1.jpg); } body#home.variation2 #wrapper { background-image: url(./img/tignish/home/feature-background-2.jpg); } body#home.variation3 #wrapper { background-image: url(./img/tignish/home/feature-background-3.jpg); } </style> </head> <body id="home" class="js platform-windows variation2"> <mce:script type="text/javascript"><!-- // Add a class to the body tag to alternate background features var class_options = new Array( "variation1", "variation2", "variation3" ); if (Math.random) { var choice = Math.floor(Math.random() * (class_options.length)); // Just in case javascript gets carried away... choice = ( (choice < class_options.length) && choice >= 0) ? choice : 0; if (document.body.className == '') { document.body.className = class_options[choice]; } else { document.body.className += ' '+class_options[choice]; } } // --></mce:script> <div id="wrapper"> </div> <%-- <form id="form1" action=""> </form>--%> </body> </html>
body#home.variation1 #wrapper { background-image: url(./img/tignish/home/feature-background-1.jpg); }
body#home.variation2 #wrapper { background-image: url(./img/tignish/home/feature-background-2.jpg); }
body#home.variation3 #wrapper { background-image: url(./img/tignish/home/feature-background-3.jpg); }
图片路径要换成你项目中的图片路径。
相关文章推荐
- JavaScript 实现背景图片随机浮动
- 网页html随机切换背景图片
- 网页html随机切换背景图片
- JavaScript控制网页背景随机不停的变换颜色
- JavaScript 实现背景图片随机浮动2
- JavaScript基础 设置图片的高度与宽度 在网页中出现一个随机大小 的太阳图片
- javascript随机显示背景图片的方法
- javascript随机显示背景图片的方法
- js实现网页随机切换背景图片的方法
- 随机切换网页背景图片的JS代码
- JavaScript基础 设置图片的高度与宽度 在网页中出现一个随机大小 的太阳图片
- javascript--样式切换,图片切换,随机背景
- javascript控制网页背景图片
- js实现网页随机切换背景图片的方法
- JavaScript基础 每隔一秒 随机变背景颜色 document.body.bgColor
- 网页中固定背景图片
- javascript实现点击按钮弹出一个可关闭层窗口同时网页背景变灰的方法
- CSS实现网页背景图片自适应全屏
- 【CSS】css网页背景图片设置
- 非常有趣的Javascript特效,能让网页上的图片飞起来:)