老师我的按键盘左右健没有反应

老师我的按键盘左右健没有反应

base.js

import {ELEMENT_NODE_TYPE} from './constants.js';

import{DEFAULTS} from './default.js';


class BaseSlider{

constructor(el,options){

if(el.nodeType!=ELEMENT_NODE_TYPE){

console.log('不是DOM元素');

console.log(el.nodeType);

}


this.options={

...DEFAULTS,

...options,

};


const sliderEL=el;

const sliderContenEL=sliderEL.querySelector('.slider-content');

const sliderItemEls=sliderContenEL.querySelectorAll('.slider-item');


this.sliderEL=sliderEL;

this.sliderContenEL=sliderContenEL;

this.sliderContentEls=sliderItemEls;



this.minIndex=0;

this.maxIndex=sliderItemEls.length-1;


this.currIndex=this.getCorrectedIndex(this.options.initialIndex);

this.itemWidth=sliderItemEls[0].offsetWidth;


this.init();

}

init(){

this.setltemsWidth();

this.setContentWidth();



this.move(this.getDistance());


if(this.options.animation){

this.openAnimation();

}

if(this.options.autoplay){

this.autoplay();

}

}

to(index){

index=this.getCorrectedIndex(index);


if(this.currIndex===index) return;


this.currIndex=index;

const distance=this.getDistance();


if(this.options.animation){

this.moveWithAnimation(distance);

this.move(distance);

}

}


prev(){

this.to(this.currIndex-1 )

}


next(){

this.to(this.currIndex+1)

}


autoplay(){

const {autoplay}=this.options;

if(this.options.autoplay<=0) return;

this.pause();

this.autoplayTimer=setInterval(()=>{

this.next();

},autoplay)

}

pause(){

clearInterval(this.autoplayTimer);

}

openAnimation(){

this.sliderContenEL.classlist.add('slider-animation');

}


moveWithAnimation(distance){

this.setAnimationSpeed();

this.move(distance);

this.sliderContentEl.addEventListtener('transitionend',()=>{

this.closeAnimation();

},false)

}

closeAnimation(){

this.setAnimathionSpeed(0);

}

setAnimationSpeed(speed=this.options.speed){

this.sliderContentEl.style.transitionDuration=`${speed}ms`

}

getDistance(index=this.currIndex){

return -this.itemWidth*index;

}


move(distance){

this.sliderContenEL.style.transform=`translate3d(${distance}px,0px,0px)`

}



setltemsWidth(){

for(const item of this.sliderContentEls){

item.style.width=`${this.itemWidth}px`

}

}


setContentWidth(){

this.sliderContenEL.style.width=`${this.itemWidth*this.sliderContentEls.length}px`

}

getCorrectedIndex(index){

if(index<this.minIndex) return this.maxIndex;

if(index>this.maxIndex) return this.minIndex;


return index;

}

}



export default BaseSlider;




constants.js

//keyboard




export const LEFT_KEYCODE=37;

export const RIGHT_KEYCODE=39;


//base

export const ELEMENT_NODE_TYPE=1;


default.js

//默认参数

const DEFAULTS={

//初始索引

initialIndex:0,

//切换时是否有动画

animation:true,

//切换速度,单位ms

speed:300,

//自动切换,单位ms

autoplay:0,

}


export default DEFAULTS;

keyboard.js

import{LEFT_KEYCODE,RIGHT_KEYCODE} from './constants.js';


const keyboard ={

bindEvent(slider){

document.addEventListener('keyup',(ev)=>{

if(ev.keyCode===LEFT_KEYCODE){

slider.prev();

}

else if(ev.keyCode===RIGHT_KEYCODE){

slider.next();

}

},false)

}

}




export default keyboard;

index.js

import BaseSlider from './base.js';

import keydoard from './keyboard.js';



class Slider extends BaseSlider{


constructor(el,options){

super(el,options);


this.bindEvent();


}


bindEvent(){

keydoard.bindEvent(this);

}

}



export default Slider;


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕星星 2022-03-25 14:00:18

同学你好,将上面的代码粘贴到链接的源码后,问题如下:

1、slider.art文件中多了一个button组件,导致获取元素不对,无法进行左右按钮点击

https://img1.sycdn.imooc.com//climg/623d59ea0904c05707650331.jpg

2、base.js中的prev方法中缺少to方法调用,否则无法切换

https://img1.sycdn.imooc.com//climg/623d5a6409c2dfd805210244.jpg

3、点击左右键盘切换,需要在slider/index.js文件中调用bindEvent方法

https://img1.sycdn.imooc.com//climg/623d5a340930146607190218.jpg

自己再测试下。

卡布琦诺 2022-02-27 15:31:25

同学你好,代码中存在如下问题,请同学仔细阅读并按照步骤进行修改

https://img1.sycdn.imooc.com//climg/621b27bc09db3fbc08950191.jpg

https://img1.sycdn.imooc.com//climg/621b282f093024a611350284.jpg

https://img1.sycdn.imooc.com//climg/621b2865094d538514820318.jpg

https://img1.sycdn.imooc.com//climg/621b28be09d008a014420323.jpg

祝学习愉快!

  • 提问者 慕尼黑0510008 #1
    当我按下左右健的时候 控制台报以下错误

    base.js?4763:99 Uncaught TypeError: Cannot read properties of undefined (reading 'style')

        at Slider.setAnimationSpeed (base.js?4763:99:1)

        at Slider.moveWithAnimation (base.js?4763:88:1)

        at Slider.to (base.js?4763:58:1)

        at Slider.next (base.js?4763:68:1)

        at HTMLDocument.eval (keyboard.js?f02a:10:1)

    setAnimationSpeed @ base.js?4763:99

    moveWithAnimation @ base.js?4763:88

    to @ base.js?4763:58

    next @ base.js?4763:68

    eval @ keyboard.js?f02a:10

    base.js?4763:99 Uncaught TypeError: Cannot read properties of undefined (reading 'style')

        at Slider.setAnimationSpeed (base.js?4763:99:1)

        at Slider.moveWithAnimation (base.js?4763:88:1)

        at Slider.to (base.js?4763:58:1)

        at Slider.prev (base.js?4763:64:1)

        at HTMLDocument.eval (keyboard.js?f02a:7:1)

    setAnimationSpeed @ base.js?4763:99

    moveWithAnimation @ base.js?4763:88

    to @ base.js?4763:58

    prev @ base.js?4763:64

    eval @ keyboard.js?f02a:7

    base.js?4763:99 Uncaught TypeError: Cannot read properties of undefined (reading 'style')

        at Slider.setAnimationSpeed (base.js?4763:99:1)

        at Slider.moveWithAnimation (base.js?4763:88:1)

        at Slider.to (base.js?4763:58:1)

        at Slider.next (base.js?4763:68:1)

        at HTMLDocument.eval (keyboard.js?f02a:10:1)

    setAnimationSpeed @ base.js?4763:99

    moveWithAnimation @ base.js?4763:88

    to @ base.js?4763:58

    next @ base.js?4763:68

    eval @ keyboard.js?f02a:10


    2022-02-28 12:55:44
  • 卡布琦诺 回复 提问者 慕尼黑0510008 #2

    同学你好,请把你的base.js的代码贴上来,你报错是因为你的代码中变量或者是属性书写不一致导致的,祝学习愉快!

    2022-02-28 13:08:42
  • 老师,麻烦帮我看一下为什么我的左右按钮点击没有反应呢?代码链接: https://pan.baidu.com/s/1nzRZ8wFa2K9DMSJAr6NBLw?pwd=v2pf 提取码: v2pf 

    2022-03-25 11:43:29
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师