简体   繁体   English

标签内容的自举滚动效果

[英]Bootstrap scroll effect for tab content

I am using bootstrap content tab with scroll effect on mobile view. 我正在使用带有移动效果滚动效果的引导程序内容选项卡。

In desktop view it is normal tab structure but when it is viewed on smaller screens, clicking on tabs it should scroll to the respective content panel. 在桌面视图中,它是正常的标签结构,但是在较小的屏幕上查看时,单击标签应滚动到相应的内容面板。

In my demo you can see that scroll works on second time click but not on first click. 在我的演示中,您可以看到滚动在第二次单击时有效,但在第一次单击时无效。 Any idea what I am missing? 知道我缺少什么吗?

Need to get the scroll effect on first click itself. 需要在首次单击时获得滚动效果。

$('.tabs-right>li a').on('click', function (e) {
        var href = $(this).attr('href');
        $('html, body').animate({
            scrollTop: $(href).offset().top
        }, 'slow');
        e.preventDefault();
    });

Updated DEMO 更新了演示

You are handling click events on the same a elements here that bootstrap uses to trigger changing tabs in the first place, and that seems to interfere here. 您在同一处理click事件a元素在这里,引导用来触发摆在首位改变标签,这似乎在这里干扰。

Use the events the tabs plugin provides instead to trigger you scrolling animation: 使用tabs插件提供事件来触发滚动动画:

$('.tabs-right>li a').on('shown.bs.tab', function (e) {
        var href = $(this).attr('href');
        $('html, body').animate({
            scrollTop: $(href).offset().top
        }, 'slow');
})

https://jsfiddle.net/b3nhjvot/220/ https://jsfiddle.net/b3nhjvot/220/

您的代码没有问题,但是使用最新版本的jQuery(3.1.1),那么它就可以正常工作了:)玩得开心

JS Fiddle: https://jsfiddle.net/arifkarim/b3nhjvot/209/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM