欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS实现头条新闻的经典轮播图效果示例

程序员文章站 2023-12-09 21:16:33
本文实例讲述了js实现头条新闻的经典轮播图效果。分享给大家供大家参考,具体如下:

本文实例讲述了js实现头条新闻的经典轮播图效果。分享给大家供大家参考,具体如下:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport"
     content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <title>document</title>
  <style>
  *{
    margin:0;
    padding:0;
    list-style: none;
  }
    .box{
      width: 665px;
      height: 362px;
      border: solid;
      margin: 100px auto;
      position:relative ;
    }
    .left{
      width: 50px;
      height: 50px;
      border: solid white;
      border-radius: 50%;
      position: absolute;
      line-height: 50px;
      text-align: center;
      font-size: 50px;
      left:0px;
      top:180px;
      cursor: pointer;
      color: white;
    }
  .right {
    width: 50px;
    height: 50px;
    border: solid white;
    border-radius: 50%;
    position: absolute;
    line-height: 50px;
    text-align: center;
    font-size: 50px;
    right: 0px;
    top: 180px;
    cursor: pointer;
    color: white;
  }
   ul{
     width: 400px;
     height: 50px;
     margin:307px 188px;
     position: absolute;
     left:30px;
     top:24px;
   }
li{
  width: 30px;
  height: 30px;
  border: solid 1px white;
  border-radius: 50%;
  float: left;
  cursor: pointer;
  line-height: 30px;
  text-align: center;
  color: white;
}
    img{
      display: none;
      width: 665px;
      height: 362px;
    }
    .act{display: block;
    }
    .active{
      background: black;
    }
  </style>
</head>
<body>
<div class="box" id="box">
  <img src="img/1.jpg" alt="" class="act">
  <img src="img/2.jpg" alt="">
  <img src="img/3.jpg" alt="">
  <img src="img/4.jpg" alt="">
  <img src="img/5.jpg" alt="">
  <img src="img/6.jpg" alt="">
  <img src="img/7.jpg" alt="">
  <img src="img/8.jpg" alt="">
  <div class="left" id="left"> < </div>
  <div class="right" id="right"> > </div>
  <ul>
    <li class="active">1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
  </ul>
</div>
<script>
  window.onload=function () {
    var arrli=document.getelementsbytagname("li");
    var arrimg=document.getelementsbytagname("img");
    var oleft=document.getelementbyid("left");
    var oright=document.getelementbyid("right");
    var obox=document.getelementbyid("box");
    var num=0;
    f=setinterval(abc,1000);
    obox.onmouseover=function () {
      clearinterval(f)
    }
    obox.onmouseout=function () {
      f=setinterval(abc,1000);
    }
    for(x=0;x<arrli.length;x++) {
      arrli[x].index=x;
      arrli[x].onmouseover=function () {
        for(j=0;j<arrli.length;j++){
          arrli[j].classname="";
          arrimg[j].classname="";
        }
        this.classname="active";
        arrimg[this.index].classname="act";
      }
    }
      oleft.onclick=function () {
        num=num-1;
        if(num<0){
          num=arrimg.length-1
        }
        for(j=0;j<arrimg.length;j++){
          arrimg[j].classname="";
          arrli[j].classname="";
        }arrimg[num].classname="act";
        arrli[num].classname="active";
      }
    oright.onclick=abc
        function abc() {
      num=num+1;
      if(num>arrimg.length-1){
        num=0
      }
      for(j=0;j<arrimg.length;j++){
        arrimg[j].classname="";
        arrli[j].classname="";
      }arrimg[num].classname="act";
      arrli[num].classname="active";
    }
  }
</script>
</body>
</html>

本机测试运行效果如下:

JS实现头条新闻的经典轮播图效果示例

更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript图片操作技巧大全》、《javascript切换特效与技巧总结》、《javascript运动效果与技巧汇总》、《javascript动画特效与技巧汇总》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》、《javascript遍历算法与技巧总结》及《javascript数学运算用法总结

希望本文所述对大家javascript程序设计有所帮助。