zoukankan      html  css  js  c++  java
  • 自己练习的无缝滚动原生JS

    看了妙趣的课堂自己练习的,因为牵涉到远程就没利用图片
     
    <!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=utf-8" />
    <title>无标题文档</title>
    <style>
      div,ul,li,a{ margin:0; padding:0}
      ul,li{list-style:none}
      .roll{width:560px; margin:50px auto;overflow:hidden; position:relative}
      .prev,.next{ width:80px;height:80px;background:#ccc; color:#fff}
      .prev{position:absolute;left:0;top:20px;}
      .next{position:absolute;right:0;top:20px;}
      .scroll{position:relative;left:100px;width:360px;height:100px;overflow:hidden}
      .scroll ul{position:absolute;left:0}
      .scroll li{width:100px;height:100px;margin:0 10px; float:left;text-align:center}
    </style>
    <script>
      window.onload = function(){
          var odiv = document.getElementById('roll');
          var oul = odiv.getElementsByTagName('ul')[0];
          var oli = odiv.getElementsByTagName('li');
          var obtn = odiv.getElementsByTagName('a')
          var speed = 30;
          var timer;
           oul.innerHTML += oul.innerHTML;
           oul.style.width = (oli[0].offsetWidth+oli[0].offsetLeft*2)*oli.length+'px';

          timer = setInterval(function(){
            oul.style.left = oul.offsetLeft+speed+'px';
             if(oul.offsetLeft < -oul.offsetWidth/2){
                  oul.style.left = '0px'
             }else if(oul.offsetLeft > 0){
                 oul.style.left = -oul.offsetWidth/2+'px'
                 }
         },300)

         obtn[0].onmouseover = function(){
            speed =-30
         }
         obtn[1].onmouseover = function(){
            speed =30
         }
         oul.onmouseover= function(){
            clearInterval(timer)
         }
         oul.onmouseout= function(){
            timer = setInterval(function(){
            oul.style.left = oul.offsetLeft+speed+'px';
             if(oul.offsetLeft < -oul.offsetWidth/2){
                  oul.style.left = '0px'
             }else if(oul.offsetLeft > 0){
                 oul.style.left = -oul.offsetWidth/2+'px'
                 }
         },300)
         }



      }
    </script>
    </head>

    <body>
       <div class="roll" id ="roll">
            <a href="###" class="prev">左侧按钮</a>
            <a href="###" class="next">右侧按钮</a>
            <div class="scroll clearfix">
                 <ul>
                     <li style="background:#f7f">第一个</li>
                     <li style="background:#0fc">第二个</li>
                     <li style="background:red">第三个</li>
                     <li style="background:green">第四个</li>
                 </ul>
            </div
       </div>
    </body>
    </html>




  • 相关阅读:
    c++ 内存管理方式
    4.2学习总结
    PTA面向对象程序设计6-3 面积计算器(函数重载)
    3.26学习总结
    PTA——c++面向对象基础
    3.17学习总结.listview用法总结
    3.16学习总结
    3.15学习总结(Python爬取网站数据并存入数据库)
    android开发使用jxl创建Excel
    第一次结对作业
  • 原文地址:https://www.cnblogs.com/zjx2011/p/2752732.html
Copyright © 2011-2022 走看看