• react TV端焦点移动插件-画廊效果实现


    react TV端焦点移动插件 react-tv-focusable

    npm
    文档

    安装

    npm i -S react-tv-focusable
    

    tv.js组件中的html(css有点多,此处就不写了):

        render(){
            const  lists = [];
            for(let i=0;i< this.list.length;i++) {
                const item = this.list[i],index = i;
                lists.push(<div className="item  r-focusable"
                 key={i} index={i}
                 style={{
                     left: -100 * index - index * 20 +'px',
                     zIndex: this.state.activeIndex === index ? 1100 :1000-Math.abs(this.state.activeIndex - index) * 5,
                     transform: `rotateY(${this.state.activeIndex < index ? '-30deg':this.state.activeIndex === index?'0deg':'30deg'}) scale(${1-Math.abs(this.state.activeIndex - index)*0.05})`,
                    }}>
                  <img src={item.url}/>
                  <span className="txt">{item.title}</span>
                </div>)
            }
            return (
                <div className="tv-box">
                <div className="item-box">
                  <div className="perspective"> {lists} </div>
                </div>
                <div className="bottom-img"><img src="../common/images/tv/r-menu.png"/></div>
              </div>
            )
        }
    

    tv.js组件中的交互

     componentDidMount() {
       	$tv.init({
             focusableClassName:'r-focusable', // 必须配置项
             distanceToCenter:true
         })
         $tv.setScrollEl(document.querySelector('.item-box'))
         $tv.requestFocus($tv.getElementByPath("//div[@class='perspective']/div[3]"));
         const els = document.querySelectorAll('.r-focusable');
         for(let el of els) {
             el.addEventListener("right", this.right.bind(this));
             el.addEventListener("left", this.left.bind(this));
             el.addEventListener("up", this.nofocus.bind(this));
             el.addEventListener("down", this.nofocus.bind(this));
         }
      }
      componentWillUnmount() {
          $tv.init({ distanceToCenter:false })
      }
      nofocus(event){ this.$tv.requestFocus(event.target); }
      right(event){
        const index = Number(event.target.getAttribute('index'));
        if(index === this.list.length - 1 ){ return; }
        this.setState({activeIndex:index + 1})
      }
      left(event){
        const index = Number(event.target.getAttribute('index'));
        if(index === 0 ){ return; }
        this.setState({activeIndex:index - 1})
      }
    

    解释:
    1.添加自定义事件left,right,按左右按键来计算当前层级以及缩放比例
    2.添加自定义事件up,down,按上下按键的时候阻止焦点跳动

    demo地址:https://github.com/slailcp/tv-focusable-example/blob/master/react-tv-focusable-example/src/views/example7.js

    最终界面:

  • 相关阅读:
    “TensorFlow 开发者出道计划”全攻略,玩转社区看这里!
    项目章程
    Android 开发环境的搭建(新环境)
    java中八种基本数据类型以及它们的封装类,String类型的一些理解
    一品黄山 天高云淡
    一品黄山 天高云淡
    黄山的日出日落
    宏村,寻找你的前世今生
    宏村,寻找你的前世今生
    git把本地文件上传到github上的步骤
  • 原文地址:https://www.cnblogs.com/darkbluelove/p/14185625.html
Copyright © 2020-2023  润新知