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


Posted in Javascript onJanuary 30, 2019

本文实例讲述了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 相关文章推荐
jQuery使用手册之一
Mar 24 Javascript
JavaScript实现统计文本框Textarea字数增强用户体验
Dec 21 Javascript
javascript实现字符串反转的方法
Feb 05 Javascript
js如何判断访问是来自搜索引擎(蜘蛛人)还是直接访问
Sep 14 Javascript
js中对函数设置默认参数值的3种方法
Oct 23 Javascript
vue.js入门教程之绑定class和style样式
Sep 02 Javascript
jQuery ready()和onload的加载耗时分析
Sep 08 Javascript
使用JavaScript实现表格编辑器(实例讲解)
Aug 02 Javascript
自定义PC微信扫码登录样式写法
Dec 12 Javascript
浅谈Angular 观察者模式理解
Nov 01 Javascript
ES6 Proxy实现Vue的变化检测问题
Jun 11 Javascript
node express使用HTML模板的方法示例
Aug 22 Javascript
AJAX在JQuery中的应用详解
Jan 30 #jQuery
JS实现换肤功能的方法实例详解
Jan 30 #Javascript
js实现ATM机存取款功能
Oct 27 #Javascript
详解基于electron制作一个node压缩图片的桌面应用
Jan 29 #Javascript
JS实现鼠标拖拽盒子移动及右键点击盒子消失效果示例
Jan 29 #Javascript
JS实现盒子跟着鼠标移动及键盘方向键控制盒子移动效果示例
Jan 29 #Javascript
JS实现的类似微信聊天效果示例
Jan 29 #Javascript
You might like
PHP邮件专题
2006/10/09 PHP
初探PHP5
2006/10/09 PHP
其他功能
2006/10/09 PHP
php中拷贝构造函数、赋值运算符重载
2012/07/25 PHP
简单谈谈php浮点数精确运算
2016/03/10 PHP
CI框架教程之优化验证码机制详解【验证码辅助函数】
2019/04/16 PHP
LBS blog sql注射漏洞[All version]-官方已有补丁
2007/08/26 Javascript
Javascript实现CheckBox的全选与取消全选的代码
2010/07/20 Javascript
基于JavaScript实现继承机制之构造函数方法对象冒充的使用详解
2013/05/07 Javascript
js将json格式内容转换成对象的方法
2013/11/01 Javascript
js 点击页面其他地方关闭弹出层(示例代码)
2013/12/24 Javascript
Javascript实现的常用算法(如冒泡、快速、鸽巢、奇偶等)
2014/04/29 Javascript
node.js中的fs.fchmod方法使用说明
2014/12/16 Javascript
javascript实现查找数组中最大值方法汇总
2016/02/13 Javascript
node网页分段渲染详解
2016/09/05 Javascript
AngularJS动态加载模块和依赖的方法分析
2016/11/08 Javascript
详解在Angular项目中添加插件ng-bootstrap
2017/07/04 Javascript
vue.js 微信支付前端代码分享
2018/02/10 Javascript
微信小程序中换行空格(多个空格)写法详解
2018/07/10 Javascript
JavaScript进阶(三)闭包原理与用法详解
2020/05/09 Javascript
前端开发基础javaScript的六大作用
2020/08/06 Javascript
Python实现端口复用实例代码
2014/07/03 Python
实例讲解Python中的私有属性
2014/08/21 Python
python实现udp数据报传输的方法
2014/09/26 Python
Python中DJANGO简单测试实例
2015/05/11 Python
全面解析Python的While循环语句的使用方法
2015/10/13 Python
Python爬虫实例_利用百度地图API批量获取城市所有的POI点
2018/01/10 Python
Python Pivot table透视表使用方法解析
2020/09/11 Python
深入解读CSS3中transform变换模型的渲染
2016/05/27 HTML / CSS
W3C公布最新的HTML5标准草案
2008/10/17 HTML / CSS
使用phonegap克隆和删除联系人的实现方法
2017/03/31 HTML / CSS
惠普美国官方商店:HP Official Store
2016/08/28 全球购物
英国拖鞋购买网站:Bedroom Athletics
2020/02/28 全球购物
《我不是最弱小的》教学反思
2014/02/23 职场文书
绿色校园广播稿
2014/10/13 职场文书
《勇者辞职不干了》ED主题曲无字幕动画MV公开
2022/04/13 日漫