首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何添加平滑过渡效果

问如何添加平滑过渡效果
EN

Stack Overflow用户
提问于 2020-08-23 13:22:15
回答 1查看 144关注 0票数 1

有谁知道我怎样才能给它添加平滑过渡效果?

​

我在一个JS文件中尝试过,但它一直在每个div周围显示一个白色的轮廓:

代码语言:javascript
复制
$(function () {
    menu = $('nav ul');

    $('#toggle-btn').on('click', function (e) {
        e.preventDefault();
        menu.slideToggle();
    });

    $(window).resize(function () {
        var w = $(this).width();
        if (w > 580 && menu.is(':hidden')) {
            menu.removeAttr('style');
        }
    });

    $('nav li').on('click', function (e) {
        var w = $(window).width();
        if (w < 580) {
            menu.slideToggle();
        }
    });
    $('.open-menu').height($(window).height());
});

// smooth scrolling

$('a[href*="#"]')
    .not('[href="#"]')
    .not('[href="#0"]')
    .click(function (event) {
        if (
            location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') &&
            location.hostname == this.hostname
        ) {
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
            if (target.length) {
                event.preventDefault();
                $('html, body').animate({
                    scrollTop: target.offset().top
                }, 1000, function () {
                    var $target = $(target);
                    $target.focus();
                    if ($target.is(":focus")) {
                        return false;
                    } else {
                        $target.attr('tabindex', '-1');
                        $target.focus();
                    };
                });
            }
        }
    });

我尝试了其他一些方法,但它们都不起作用。有没有办法用CSS做到这一点?或者只能使用JS来完成?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-23 13:29:32

您可以使用css添加平滑滚动效果:

代码语言:javascript
复制
html {
  scroll-behavior: smooth;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63543675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档