亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JS實現(xiàn)輪播圖案例

 更新時間:2022年04月02日 09:11:55   作者:weixin_51276799  
這篇文章主要為大家詳細介紹了JS實現(xiàn)輪播圖案例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了JS實現(xiàn)輪播圖案例的具體代碼,供大家參考,具體內容如下

實現(xiàn)功能:

1、自動輪播:鼠標停留在輪播圖上時不切換圖片,鼠標離開后自動輪播。

2、點擊左右按鈕切換圖片。

3、點擊下方按鈕切換到對應的圖片。

4、輪播圖大小自適應:

可以放入到執(zhí)行的父容器中展示。

不指定父容器時,默認放入body標簽。占滿一屏的寬度,當改變?yōu)g覽器窗口大小時,輪播圖大小成比例改變。

可以指定輪播圖的寬高。

實現(xiàn)方式:用面向對象的方式實現(xiàn),使用時傳入圖片和圖片對應的數(shù)據(jù),再創(chuàng)建實例。

import Carousel from './js/Carousel.js';
? var itemList1 = [{
? ? ? ? ? ? ? ? day: 26,
? ? ? ? ? ? ? ? date: "/Oct.2020",
? ? ? ? ? ? ? ? title: "秘境之藍 無阿里不西藏 自駕阿里小北線",
? ? ? ? ? ? ? ? src: "./carousel_img/a.jpg",
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? ? day: 25,
? ? ? ? ? ? ? ? date: "/Oct.2020",
? ? ? ? ? ? ? ? title: "這是一個什么樣的國家?",
? ? ? ? ? ? ? ? src: "./carousel_img/b.jpg",
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? ? day: 24,
? ? ? ? ? ? ? ? date: "/Oct.2020",
? ? ? ? ? ? ? ? title: "在徽州,給秋天寫了8封信",
? ? ? ? ? ? ? ? src: "./carousel_img/c.jpg",
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? ? day: 23,
? ? ? ? ? ? ? ? date: "/Oct.2020",
? ? ? ? ? ? ? ? title: "「穿過狂野的風」趕赴內蒙的追羊計劃",
? ? ? ? ? ? ? ? src: "./carousel_img/d.jpg",
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? ? day: 22,
? ? ? ? ? ? ? ? date: "/Oct.2020",
? ? ? ? ? ? ? ? title: "愛讓我們無所不能|南極大冒險",
? ? ? ? ? ? ? ? src: "./carousel_img/e.jpg",
? ? ? ? ? ? },
? ? ? ? ];
? ? ? ? let carousel1 = new Carousel(itemList1);
? ? ? ? carousel1.appendTo(".div1");
? ? ? ? animation();
? ? ? ? function animation(){
? ? ? ? ? ? requestAnimationFrame(animation);
? ? ? ? ? ? carousel1.update();
? ? ? ? ? ? // carousel2.update();
}

代碼:

import Component from './Component.js';
?
export default class Carousel extends Component{
? ??
? ? imgList;
? ? bnList;
? ? imgCon;
? ? dot;
? ? dotList=[];
? ? data;
? ? direction;
? ? pos=0;
? ? x=0;
? ? speedX=1;
? ? bool=false;
? ? time=200;
? ? autoBool=true;
?
? ? // WIDTH=13.66;
? ? // HEIGHT=4.55;
? ? WIDTH;
? ? HEIGHT;
? ??
? ? constructor(_data,_width,_height){
? ? ? ? super("div");
? ? ? ? this.data=_data;
? ? ? ? this.width = _width;
? ? ? ? this.height = _height;
? ? ? ? this.elem.className = "carousel";
? ? ? ? // Object.assign(this.elem.style,{
? ? ? ? // ? ? width:this.WIDTH+"rem",
? ? ? ? // ? ? height:this.HEIGHT+"rem",
? ? ? ? // ? ? position:"relative",
? ? ? ? // ? ? overflow:"hidden",
? ? ? ? // });
? ? ? ? let arr = ["./carousel_img/left.png","./carousel_img/right.png"];
? ? ? ? let _imgList = this.data.reduce((value,item)=>{
? ? ? ? ? ? if(item.src) value.push(item.src);
? ? ? ? ? ? return value
? ? ? ? },arr);
? ? ? ? this.loadImg(_imgList,this.createCarousel);
? ? ? ? // window.addEventListener("resize",e=>this.resizeHandler(e));
? ? }
?
? ? createCarousel(imgList){
?
? ? ? ? Object.assign(this.elem.style,{
? ? ? ? ? ? width:this.WIDTH+"rem",
? ? ? ? ? ? height:this.HEIGHT+"rem",
? ? ? ? ? ? position:"relative",
? ? ? ? ? ? overflow:"hidden",
? ? ? ? });
?
? ? ? ? this.imgList = imgList;
? ? ? ? this.bnList = this.imgList.splice(0,2);
?
? ? ? ? imgList.forEach(item=>{
? ? ? ? ? ? Object.assign(item.style,{
? ? ? ? ? ? ? ? width:this.WIDTH+"rem",
? ? ? ? ? ? ? ? height:this.HEIGHT+"rem",
? ? ? ? ? ? })
? ? ? ? })
?
? ? ? ? this.createimgCon();
? ? ? ? this.createDotList();
? ? ? ? this.createBn();
? ? ? ? // 動畫一般在外面做,類里面只需要寫狀態(tài)更新即可。
? ? ? ? // this.animation();
?
? ? ? ? // 鼠標停留在輪播圖上時不進行自動輪播。
? ? ? ? this.elem.addEventListener("mouseenter",e=>this.mouseHandler(e));
? ? ? ? this.elem.addEventListener("mouseleave",e=>this.mouseHandler(e));
? ? }
? ? createimgCon(){
? ? ? ? this.imgCon = document.createElement("div");
? ? ? ? Object.assign(this.imgCon.style,{
? ? ? ? ? ? width:this.WIDTH*2+"rem",
? ? ? ? ? ? height:this.HEIGHT+"rem",
? ? ? ? ? ? position:"absolute",
? ? ? ? });
? ? ? ? let item = this.createItem(this.imgList[0],this.data[0]);
? ? ? ? this.imgCon.appendChild(item);
? ? ? ? this.elem.appendChild(this.imgCon);
? ? }
? ? createItem(img,obj){ ? ?
? ? ? ? let item = document.createElement("div");
? ? ? ? Object.assign(item.style,{
? ? ? ? ? ? width:this.WIDTH+"rem",
? ? ? ? ? ? height:this.HEIGHT+"rem",
? ? ? ? ? ? position:"relative",
? ? ? ? ? ? float:"left",
? ? ? ? });
? ? ? ? let title = document.createElement("div");
? ? ? ? Object.assign(title.style,{
? ? ? ? ? ? position:"absolute",
? ? ? ? ? ? left:"15%",
? ? ? ? ? ? top:"0.3rem",
? ? ? ? ? ? fontSize:"0.3rem",
? ? ? ? ? ? color:"#ffffff",
? ? ? ? ? ? textShadow:"0.02rem 0.02rem 0.02rem #000000",
? ? ? ? ? ? width:"8rem",
? ? ? ? ? ? lineHeight:"0.5rem",
? ? ? ? })
? ? ? ? let head1=document.createElement("div");
? ? ? ? Object.assign(head1.style,{
? ? ? ? ? ? height:"0.5rem",
? ? ? ? })
? ? ? ? head1.textContent = obj.date;
? ? ? ? let span = document.createElement("span");
? ? ? ? Object.assign(span.style,{
? ? ? ? ? ? fontSize:"0.4rem",
? ? ? ? });
? ? ? ? span.textContent = obj.day;
? ? ? ? let head2 = document.createElement("div");
? ? ? ? Object.assign(head2.style,{
? ? ? ? ? ? height:"0.5rem",
? ? ? ? })
? ? ? ? head2.textContent = obj.title;
? ? ? ? head1.insertBefore(span,head1.firstChild);
? ? ? ? title.appendChild(head1);
? ? ? ? title.appendChild(head2);
? ? ? ? item.appendChild(title);
? ? ? ? item.appendChild(img);
?
? ? ? ? return item;
? ? }
? ? createDotList(){
? ? ? ? this.dot = document.createElement("ul");
? ? ? ? Object.assign(this.dot.style,{
? ? ? ? ? ? listStyle:"none",
? ? ? ? ? ? margin:0,
? ? ? ? ? ? padding:0,
? ? ? ? ? ? position:"absolute",
? ? ? ? ? ? left:(this.WIDTH-1.8)/2+"rem",
? ? ? ? ? ? bottom:"0.3rem", ? ? ? ? ? ?
? ? ? ? })
? ? ? ? for(let i=0;i<this.imgList.length;i++){
? ? ? ? ? ? let li = document.createElement("li");
? ? ? ? ? ? Object.assign(li.style,{
? ? ? ? ? ? ? ? width:"0.18rem",
? ? ? ? ? ? ? ? height:"0.18rem",
? ? ? ? ? ? ? ? borderRadius:"0.2rem",
? ? ? ? ? ? ? ? marginLeft:i===0?"0px":"0.2rem",
? ? ? ? ? ? ? ? border:"0.02rem solid red",
? ? ? ? ? ? ? ? float:"left",
? ? ? ? ? ? })
? ? ? ? ? ? this.dot.appendChild(li);
? ? ? ? ? ? this.dotList.push(li);
? ? ? ? }
? ? ? ? this.dot.addEventListener("click",e=>this.dotClickHandler(e));
? ? ? ? this.elem.appendChild(this.dot);
? ? }
? ? createBn(){
? ? ? ? for(let i=0;i<this.bnList.length;i++){
? ? ? ? ? ? Object.assign(this.bnList[i].style,{
? ? ? ? ? ? ? ? position:"absolute",
? ? ? ? ? ? ? ? top:(this.HEIGHT*100-this.bnList[i].height)/2/100+"rem",
? ? ? ? ? ? })
? ? ? ? ? ? if(i===0){
? ? ? ? ? ? ? ? this.bnList[i].style.left = "0.5rem";
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? this.bnList[i].style.right = "0.5rem";
? ? ? ? ? ? }
? ? ? ? ? ? this.bnList[i].addEventListener("click",e=>this.bnClickHandler(e));
? ? ? ? ? ? this.elem.appendChild(this.bnList[i]);
? ? ? ? }
? ? }
? ? bnClickHandler(e){
? ? ? ? if(this.bool) return
? ? ? ? if(e.target===this.bnList[0]){
? ? ? ? ? ? this.direction="left";
? ? ? ? ? ? this.pos++;
? ? ? ? ? ? if(this.pos>this.imgList.length-1) this.pos = 0;
? ? ? ? }else{
? ? ? ? ? ? this.direction="right";
? ? ? ? ? ? this.pos--;
? ? ? ? ? ? if(this.pos<0) this.pos=this.imgList.length-1;
? ? ? ? }
? ? ? ? this.bool=true;
? ? ? ? this.createNextItem();
? ? }
? ? dotClickHandler(e){
? ? ? ? if(e.target.constructor!==HTMLLIElement) return ? ?//這里因為是對父元素進行偵聽,因此要先判斷點擊的是不是li,如果點擊的不是小圓點就不能改變開關,直接return。不能先改變開關。
? ? ? ? if(this.bool)return
? ? ? ? for(let i=0;i<this.dotList.length;i++){
? ? ? ? ? ? if(e.target===this.dotList[i]){
? ? ? ? ? ? ? ? if(this.pos===i) return
? ? ? ? ? ? ? ? this.direction=i<this.pos?"right":"left";
? ? ? ? ? ? ? ? this.pos=i;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? this.bool=true;
? ? ? ? this.createNextItem();
? ? }
? ? createNextItem(){
? ? ? ? let nextItem=this.createItem(this.imgList[this.pos],this.data[this.pos]);
? ? ? ? if(this.direction==="left"){
? ? ? ? ? ? this.imgCon.appendChild(nextItem);
? ? ? ? ? ? this.x=0;
? ? ? ? }else{
? ? ? ? ? ? this.imgCon.insertBefore(nextItem,this.imgCon.firstChild);
? ? ? ? ? ? this.x=-this.WIDTH;
? ? ? ? }
? ? ? ? this.imgCon.style.left=this.x+"rem";
? ? }
? ? update(){
? ? ? ? this.imgMove();
? ? ? ? this.autoPlay();
? ? }
? ? // 這里只需要做一個狀態(tài)更新即可。
? ? // 動畫在外面做。
? ? // animation(){
? ? // ? ? requestAnimationFrame(this.animation);
? ? // ? ? if(!this.bool) return
? ? // ? ? this.imgMove();
? ? // }
? ? imgMove(){
? ? ? ? if(!this.bool) return
? ? ? ? if(this.direction==="left"){
? ? ? ? ? ? this.x-=this.speedX;
? ? ? ? ? ? if(this.x<-this.WIDTH){
? ? ? ? ? ? ? ? this.imgCon.firstElementChild.remove();
? ? ? ? ? ? ? ? this.x=0;
? ? ? ? ? ? ? ? this.bool=false;
? ? ? ? ? ? }
? ? ? ? }else{
? ? ? ? ? ? this.x+=this.speedX;
? ? ? ? ? ? if(this.x>0){
? ? ? ? ? ? ? ? this.imgCon.lastElementChild.remove();
? ? ? ? ? ? ? ? this.x=0;
? ? ? ? ? ? ? ? this.bool=false;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? this.imgCon.style.left=this.x+"rem";
? ? }
? ? autoPlay(){
? ? ? ? if(!this.autoBool) return
? ? ? ? this.time--; ? ?//增加防抖
? ? ? ? if(this.time===0){ ??
? ? ? ? ? ? let evt = new Event("click");
? ? ? ? ? ? this.bnList[0].dispatchEvent(evt);
? ? ? ? ? ? this.time=200;
? ? ? ? }
? ? }
? ? resizeHandler(e){
? ? ? ? document.documentElement.style.fontSize=document.documentElement.clientWidth/(this.WIDTH*100)*100+"px";
? ? }
? ? appendTo(parent){
? ? ? ? if(typeof parent==="string") parent = document.querySelector(parent);
? ? ? ? parent.appendChild(this.elem);
? ? ? ? if(!isNaN(this.WIDTH) && !isNaN(this.HEIGHT)) return
? ? ? ? if(parent===document.body){
? ? ? ? ? ? this.WIDTH = 13.66;
? ? ? ? ? ? this.HEIGHT = 4.55;
? ? ? ? }else{
? ? ? ? ? ? let rect = parent.getBoundingClientRect();
? ? ? ? ? ? this.WIDTH=rect.width/100;
? ? ? ? ? ? this.HEIGHT=rect.height/100;
? ? ? ? }
? ? }
? ? mouseHandler(e){
? ? ? ? if(e.type==="mouseenter"){
? ? ? ? ? ? this.autoBool=false;
? ? ? ? }else{
? ? ? ? ? ? this.autoBool=true;
? ? ? ? }
? ? }
?
? ? // 圖片預加載
? ? loadImg(_imgList,_callBack){
? ? ? ? let img = new Image();
? ? ? ? img.i=0;
? ? ? ? img.arr=[];
? ? ? ? img.imgList=_imgList;
? ? ? ? // img.callBack=_callBack;
? ? ? ? img.src=_imgList[img.i];
? ? ? ? img.addEventListener("load",e=>this.imgLoadFinishHandler(e));
? ? }
? ? imgLoadFinishHandler(e){
? ? ? ? // console.log(e.currentTarget);
? ? ? ? e.currentTarget.arr.push(e.currentTarget.cloneNode());
? ? ? ? e.currentTarget.i++;
? ? ? ? if(e.currentTarget.i<e.currentTarget.imgList.length){
? ? ? ? ? ? e.currentTarget.src = e.currentTarget.imgList[e.currentTarget.i];
? ? ? ? }else{
? ? ? ? ? ? // e.currentTarget.callBack(e.currentTarget.arr);
? ? ? ? ? ? this.createCarousel(e.currentTarget.arr);
? ? ? ? }
? ? }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • contenteditable可編輯區(qū)域設置換行實現(xiàn)技巧實例

    contenteditable可編輯區(qū)域設置換行實現(xiàn)技巧實例

    這篇文章主要為大家介紹了contenteditable可編輯區(qū)域設置換行實現(xiàn)技巧實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • js+canvas實現(xiàn)轉盤效果(兩個版本)

    js+canvas實現(xiàn)轉盤效果(兩個版本)

    這篇文章主要為大家詳細介紹了兩個版本的js+canvas實現(xiàn)轉盤效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • 一個js過濾空格的小函數(shù)

    一個js過濾空格的小函數(shù)

    過濾空格,尤其是在一些注冊頁面比較實用,可以用js在客戶端將空格過濾掉,減輕服務器端的負擔,下面是實現(xiàn)函數(shù)
    2014-10-10
  • JavaScript?中創(chuàng)建私有成員

    JavaScript?中創(chuàng)建私有成員

    這篇文章主要介紹了JavaScript?中創(chuàng)建私有成員,下面介紹以?#?作為前綴幾種在?JavaScript?代碼中實現(xiàn)私有屬性和方法的方式,需要的小伙伴可以參考一下
    2021-12-12
  • Javascript Request獲取請求參數(shù)如何實現(xiàn)

    Javascript Request獲取請求參數(shù)如何實現(xiàn)

    使用Javascript Request獲取參數(shù)的時候總是提示出錯,本文為此問題提供詳細的解決方案,需要了解的朋友可以參考下
    2012-11-11
  • 微信小程序表單驗證功能完整實例

    微信小程序表單驗證功能完整實例

    這篇文章主要介紹了微信小程序表單驗證功能,結合完整實例形式分析了微信小程序完成表單驗證功能所涉及的視圖與邏輯操作技巧,需要的朋友可以參考下
    2017-12-12
  • Js如何判斷客戶端是PC還是手持設備簡單分析

    Js如何判斷客戶端是PC還是手持設備簡單分析

    在工作過程中,許多朋友會經(jīng)常用到js判斷客戶端是PC還是手持設備,今天將提供以下方法,需要的朋友可以參考下
    2012-11-11
  • 原生js實現(xiàn)查詢天氣小應用

    原生js實現(xiàn)查詢天氣小應用

    這篇文章主要為大家詳細介紹了原生js實現(xiàn)查詢天氣的小應用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • js函數(shù)般調用正則

    js函數(shù)般調用正則

    Firefox 包含了一個非標準的 JavaScript 擴展,使正則像函數(shù)一樣可調用。
    2008-04-04
  • uni-app使用組件的步驟記錄

    uni-app使用組件的步驟記錄

    這篇文章主要給大家介紹了關于uni-app使用組件的詳細步驟,文中還介紹了自定義組件的使用方法,本文通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2023-08-08

最新評論