vue/react的hash模式下的锚点效果

就是如链接所示:http://oa-img.oss-cn-qingdao.aliyuncs.com/up-img/pic-60044/xknTNnFHsxFaEJYJ3P5ZD82jnfXWTjHj.png,点击有好看的滚动效果,代码如下:

html:

 <div className="carBrandStyle" style={{display:start?'block':'none',right:start?'0':'-110%'}}>
        <ul id="ps" style={{marginTop: 0}}>
            <li className="commonCarTitle text_center font_bold flex relative">
                <i className="iconfont icon-angleleft absolute font20" onClick={t.closeAll.bind(t,'all')}></i>
                <span className="align_self_center">品牌</span>
            </li>
            {
                brandList.map((items, index) => (
                    <div>
                        <li className="byWords flex bg_ddd justify_content_center" id={words[index]}>
                            <span className="align_self_center font_bold fcolor_orange">{words[index]}</span>
                        </li>
                        {
                            items.map((item, index) => (
                                <li className="flex" onClick={t.getCarSeries.bind(t,item.brand_id,item.brand_name)}>
                                    <div className="flex align_self_center">
                                        <div className="carBrandImg align_self_center">
                                            <img src={item.car_logo_url} alt=""/>
                                        </div>
                                        <div className=" align_self_center">{item.brand_name}</div>
                                    </div>
                                </li>
                            ))
                        }
                    </div>
                ))
            }
        </ul>
        <div className="rnm" style={{right:start?'0':'-110%'}}>
            {
                words.map((item,index) => (
                    <div>
                        <a onClick={this.goScroll.bind(this,item)}>{item}</a>
                    </div>
                ))
            }
        </div>
    </div>
                
    css: 
  .carBrandStyle  {
  position: absolute;
  z-index: 1000;
  top: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  transition: all .3s linear;
  width: 100%;
  -webkit-overflow-scrolling: touch
}
.carBrandStyle ul  {
  height: 100%;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch
}
 .commonCarList .carBrandStyle li {
  height: 40px;
  border-bottom: 1px solid #ddd;
  padding-left: 10px;
}
    
js:这才是重点吧
    goScroll(word){
        let total = (document.getElementById(word)).offsetTop;
        let container = document.getElementById('ps');//想要scrollTop生效必须让容器是height:100%;overflow:scroll才行。
        let distance = container.scrollTop;
        // 平滑滚动,时长500ms,每10ms一跳,共50跳
        let step = total / 50;
        if (total > distance) {
            smoothDown()
        } else {
            let newTotal = distance - total;
            step = newTotal / 50;
            smoothUp()
        }
        function smoothDown () {
            if (distance < total) {
                distance += step;
                container.scrollTop = distance;
                setTimeout(smoothDown, 10);
            } else {
                container.scrollTop = total;
            }
        }
        function smoothUp () {
            if (distance > total) {
                distance -= step;
                container.scrollTop = distance;
                setTimeout(smoothUp, 10)
            } else {
                container.scrollTop = total;
            }
        }
    }
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值