用JavaScript+CSS設計和列印多頁抽獎編號

來源:互聯網
上載者:User

周末的時候公司到一個學校做一場活動,活動中間有抽獎環節,抽獎是按照會議室坐位編號來的,就是把編有座位號的紙條折起來放在一個抽獎箱中。如何快速設計並列印出這些編號呢?這個問題難倒了我們美女設計。她是用設計軟體的重製方法,快速鍵通常是ctrl+d或ctrl+alt+d。如果重製的每個圖形一樣,那樣通過這個重製辦法再加上分布與排序功能可以快速做到,但是中間的號碼是變化的,這樣重製好了樣板還要一個一個地改中間的數字,麻煩。

適好此時我靈機一動,用以下方法實現了,現拿出來與大家分享(方法實用,因為無須高品質列印)。實現原理是用JavaScript動態產生編號,用CSS的page-break-after屬性實現分頁列印。這裡還要準備好這樣一張背景圖片:

馬上來看看代碼:

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>用JavaScript+CSS設計和列印多頁抽獎編號</title>
<style type="text/css">
span {display:block; width:200px; height:200px; line-height:200px; font-size:60px; font-weight:bold; text-align:center; font-family:Arial; float:left; border:1px dashed #CCC; background:url(http://p.blog.csdn.net/images/p_blog_csdn_net/webflash/EntryImages/20090419/NumBg.jpg) no-repeat}
@media print{.NextPage {page-break-after:always}}
</style>
</head>

<body>
<script type="text/javascript">
for (var i=1;i<=192;i++)
{
    if (i%12==0)
        document.write('<span class="NextPage">'+i+'</span>');
    else
        document.write('<span>'+i+'</span>');
}
</script>
</body>
</html>

 

為了列印背景,還需要一點設定,FF和IE的設定如下:

後來發現以上代碼在IE7下是無法正常工作的,最終把JavaScript做了點修改:加“i!=192”判斷是為了不要在最後產生一個空頁,其它修改是為了讓分頁列印在IE7有效。

for (var i=1;i<=192;i++)
{
    if (i%12==0&&i!=192)
        document.write('<span>'+i+'</span><div class="NextPage" style="clear:both"></div>');
    else
        document.write('<span>'+i+'</span>');
}
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.