效果图:
页面代码:
<style> *, :after, :before { margin: 0; padding: 0; box-sizing: border-box; } * { margin: 0; padding: 0; } table { border-spacing: 0; border-collapse: collapse; text-align: center; } .draw { width: 560px; height: 600px; margin: 0 auto; padding:51px; background-image: url(images/bg.png); background-repeat: no-repeat; background-size: 100%; } .draw .item { width: 150px; height: 150px; background-image: url(images/bg1.png); background-repeat: no-repeat; background-size: 96%; } .draw .item.active { background-image: url(images/bg2.png); background-repeat: no-repeat; background-size: 100%; } .draw .img { display: table-cell; width: 150px; height: 61px; vertical-align: middle; text-align: center; } .draw .img img { vertical-align: top; } .draw .gap { width: 5px; } .draw .gap-2 { height: 5px; } </style>
<!--布局除table外也可用ul>li--> <body style="background: #f02d2f;"> <div class="draw" id="lottery"> <table> <tr> <td class="item lottery-unit lottery-unit-2"> <div class="img"> <img src="images/img1.png" alt=""> </div> </td> <td class="gap"></td> <td class="item lottery-unit lottery-unit-1"> <div class="img"> <img src="images/img2.png" alt=""> </div> </td> <td class="gap"></td> <td class="item lottery-unit lottery-unit-2"> <div class="img"> <img src="images/img3.png" alt=""> </div> </td> </tr> <tr> <td class="gap-2" colspan="5"></td> </tr> <tr> <td class="item lottery-unit lottery-unit-3"> <div class="img"> <img src="images/img4.png" alt=""> </div> </td> <td class="gap"></td> <td class="item lottery-unit lottery-unit-0"> <div class="img"> <img src="images/img5.png" alt=""> </div> </td> <td class="gap"></td> <td class="item lottery-unit lottery-unit-3"> <div class="img"> <img src="images/img5.png" alt=""> </div> </td> </tr> <tr> <td class="gap-2" colspan="5"></td> </tr> <tr> <td class="item lottery-unit lottery-unit-4"> <div class="img"> <img src="images/img7.png" alt=""> </div> </td> <td class="gap"></td> <td class="item lottery-unit lottery-unit-5"> <div class="img"> <img src="images/img6.png" alt=""> </div> </td> <td class="gap"></td> <td class="item lottery-unit lottery-unit-4"> <div class="img"> <img src="images/img8.png" alt=""> </div> </td> </tr> </table> </div>
<script src="js/jquery-2.2.0.min.js" type="text/javascript"></script> <script type="text/javascript"> var lottery = { index: 0, //当前转动到哪个位置,起点位置 count: 0, //总共有多少个位置 timer: 0, init: function(id) { if($('#' + id).find('.lottery-unit').length >= 0) { $lottery = $('#' + id); $units = $lottery.find('.lottery-unit'); this.obj = $lottery; this.count = $units.length; $lottery.find('.lottery-unit.lottery-unit-' + this.index).addClass('active'); }; }, roll: function() { var index = this.index; var count = this.count; var lottery = this.obj; $(lottery).find('.lottery-unit.lottery-unit-' + index).removeClass('active'); index += 1; if(index >= count) { index = 0; } this.index = index; $(lottery).find('.lottery-unit.lottery-unit-' + index).addClass('active'); return false; }, }; window.onload = function() { lottery.init('lottery'); var ref=null; ref=setInterval(function(){ lottery.times += 1; lottery.roll(); lottery.times = 0; return false; },300) }; </script> </body>