JavaScript -窗口.滚动({行为:“smooth”})在Safari中不起作用

qzlgjiam  于 2023-01-19  发布在  Java
关注(0)|答案(9)|浏览(281)

正如标题所说,它在Chrome上运行得非常好。但在Safari上,它只是将页面设置到所需的顶部和左侧位置。这是预期的行为吗?有没有办法让它运行得很好?

svgewumm

svgewumm1#

使用smoothscroll polyfill(适用于所有浏览器的解决方案),易于应用且具有轻量级依赖性:https://github.com/iamdustan/smoothscroll
一旦你通过npm或yarn安装了它,就把它添加到你的main*.js,.ts* 文件中(首先执行的文件)

import smoothscroll from 'smoothscroll-polyfill';
// or if linting/typescript complains
import * as smoothscroll from 'smoothscroll-polyfill';

// kick off the polyfill!
smoothscroll.polyfill();
i5desfxk

i5desfxk2#

IE/Edge/Safari不完全支持行为选项,所以你必须自己实现一些东西。我相信jQuery已经有了一些东西,但是如果你没有使用jQuery,这里有一个纯JavaScript实现:

function SmoothVerticalScrolling(e, time, where) {
    var eTop = e.getBoundingClientRect().top;
    var eAmt = eTop / 100;
    var curTime = 0;
    while (curTime <= time) {
        window.setTimeout(SVS_B, curTime, eAmt, where);
        curTime += time / 100;
    }
}

function SVS_B(eAmt, where) {
    if(where == "center" || where == "")
        window.scrollBy(0, eAmt / 2);
    if (where == "top")
        window.scrollBy(0, eAmt);
}

如果您需要水平滚动:

function SmoothHorizontalScrolling(e, time, amount, start) {
    var eAmt = amount / 100;
    var curTime = 0;
    var scrollCounter = 0;
    while (curTime <= time) {
        window.setTimeout(SHS_B, curTime, e, scrollCounter, eAmt, start);
        curTime += time / 100;
        scrollCounter++;
    }
}

function SHS_B(e, sc, eAmt, start) {
    e.scrollLeft = (eAmt * sc) + start;
}

一个示例调用是:

SmoothVerticalScrolling(myelement, 275, "center");
mrphzbgm

mrphzbgm3#

关于平滑滚动的更全面的方法列表,请参见我的答案here
window.requestAnimationFrame可用于在精确的时间量内执行平滑滚动。
为了平滑地垂直滚动,可以使用以下函数。注意,水平滚动可以用大致相同的方式完成。

/*
   @param time: the exact amount of time the scrolling will take (in milliseconds)
   @param pos: the y-position to scroll to (in pixels)
*/
function scrollToSmoothly(pos, time) {
    var currentPos = window.pageYOffset;
    var start = null;
    if(time == null) time = 500;
    pos = +pos, time = +time;
    window.requestAnimationFrame(function step(currentTime) {
        start = !start ? currentTime : start;
        var progress = currentTime - start;
        if (currentPos < pos) {
            window.scrollTo(0, ((pos - currentPos) * progress / time) + currentPos);
        } else {
            window.scrollTo(0, currentPos - ((currentPos - pos) * progress / time));
        }
        if (progress < time) {
            window.requestAnimationFrame(step);
        } else {
            window.scrollTo(0, pos);
        }
    });
}

演示:
x一个一个一个一个x一个一个二个一个x一个一个三个一个
对于更复杂的情况,可以使用SmoothScroll.js library,它可以处理垂直和水平方向的平滑滚动、在其他容器元素内部的滚动、不同的缓动行为、从当前位置相对滚动等等,它还支持大多数没有原生平滑滚动的浏览器。

var easings = document.getElementById("easings");
for(var key in smoothScroll.easing){
    if(smoothScroll.easing.hasOwnProperty(key)){
        var option = document.createElement('option');
        option.text = option.value = key;
        easings.add(option);
    }
}
document.getElementById('to-bottom').addEventListener('click', function(e){
    smoothScroll({yPos: 'end', easing: easings.value, duration: 2000});
});
document.getElementById('to-top').addEventListener('click', function(e){
    smoothScroll({yPos: 'start', easing: easings.value, duration: 2000});
});
<script src="https://cdn.jsdelivr.net/gh/LieutenantPeacock/SmoothScroll@1.2.0/src/smoothscroll.min.js" integrity="sha384-UdJHYJK9eDBy7vML0TvJGlCpvrJhCuOPGTc7tHbA+jHEgCgjWpPbmMvmd/2bzdXU" crossorigin="anonymous"></script>
<!-- Taken from one of the library examples -->
Easing: <select id="easings"></select>
<button id="to-bottom">Scroll To Bottom</button>
<br>
<button id="to-top" style="margin-top: 5000px;">Scroll To Top</button>
nhn9ugyo

nhn9ugyo4#

以上这些变通方法弥补了Safari对行为支持的不足。
仍然有必要检测何时需要变通方案。
这个小函数将检测浏览器是否支持平滑滚动,在Safari上返回false,在Chrome和Firefox上返回true:

// returns true if browser supports smooth scrolling
const supportsSmoothScrolling = () => {
  const body = document.body;
  const scrollSave = body.style.scrollBehavior;
  body.style.scrollBehavior = 'smooth';
  const hasSmooth = getComputedStyle(body).scrollBehavior === 'smooth';
  body.style.scrollBehavior = scrollSave;
  return hasSmooth;
};

x一个一个一个一个x一个一个二个x

更新

Safari技术预览版本139(Safari 15.4)测试显示支持scrollBehavior smooth,因此我们可能会在15.4中看到支持。

trnvg8h3

trnvg8h35#

具有最平滑性能的解决方案是使用requestAnimationFrame,特别是在您希望合并缓动的情况下:

const requestAnimationFrame = window.requestAnimationFrame ||
          window.mozRequestAnimationFrame ||
          window.webkitRequestAnimationFrame ||
          window.msRequestAnimationFrame;

const step = (timestamp) => {
  window.scrollBy(
    0,
    1, // or whatever INTEGER you want (this controls the speed)
  );

  requestAnimationFrame(step);
};

requestAnimationFrame(step);

如果你想稍后取消滚动,你需要有一个对你的requestAnimationFrame的引用(在你使用requestAnimationFrame(step)的任何地方都这样做):

this.myRequestAnimationFrame = requestAnimationFrame(step);

const cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame;
cancelAnimationFrame(this.myRequestAnimationFrame);

现在,如果您想对滚动使用缓动,并在滚动操作之间设置超时,该怎么办?

创建一个包含60个元素的数组(requestAnimationFrame通常每秒调用60次。从技术上讲,这与浏览器的刷新率无关,但60是最常见的数字。)我们将以非线性方式填充此数组,然后使用这些数字来控制requestAnimationFrame每一步的滚动量:

let easingPoints = new Array(60).fill(0)

选择一个缓动函数。假设我们在做一个三次缓动:

function easeCubicOut(t) {
    return --t * t * t + 1;
}

创建一个虚拟数组并用通过easing函数传输的数据填充它。稍后你就会明白为什么我们需要它:

// easing function will take care of decrementing t at each call (too lazy to test it at the moment. If it doesn't, just pass it a decrementing value at each call)
    let t = 60;
    const dummyPoints = new Array(60).fill(0).map(()=> easeCubicOut(t));
    const dummyPointsSum = dummyPoints.reduce((a, el) => {
                                a += el;
                               return a;
                           }, 0);

Map缓减点使用每个虚拟点的帮助与虚拟点之比总和:

easingPoints = easingPoints.map((el, i) => {
        return Math.round(MY_SCROLL_DISTANCE * dummyPoints[i] / dummyPointsSum);
    });

在滚动功能中,我们将进行一些调整:

const requestAnimationFrame = window.requestAnimationFrame ||
              window.mozRequestAnimationFrame ||
              window.webkitRequestAnimationFrame ||
              window.msRequestAnimationFrame;

     let i = 0;
     const step = (timestamp) => {
       window.scrollBy(
         0,
         easingPoints[i],
       );

        if (++i === 60) {
                i = 0;
                return setTimeout(() => {
                  this.myRequestAnimationFrame = requestAnimationFrame(step);
                }, YOUR_TIMEOUT_HERE);
        }
      };

      this.myRequestAnimationFrame = requestAnimationFrame(step);
k4emjkb1

k4emjkb16#

适用于Safari的一个简单jQuery修复:

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

4ioopgfo7#

结合George Daniel和terrymorse的回答,下面的代码可以用于所有使用原生JavaScript的浏览器支持。
由于,Chrome,Firefox支持CSS,scroll-behavior: smooth;对于不支持此属性的浏览器,我们可以在下面添加。
超文本:

<a onclick="scrollToSection(event)" href="#section">
    Redirect On section
</a>
  
<section id="section">
  Section Content
</section>

CSS:

body {
  scroll-behavior: smooth;
}

JavaScript语言:

function scrollToSection(event) {
  if (supportsSmoothScrolling()) {
    return;
  }
  event.preventDefault();
  const scrollToElem = document.getElementById("section");
  SmoothVerticalScrolling(scrollToElem, 300, "top");
}

function supportsSmoothScrolling() {
  const body = document.body;
  const scrollSave = body.style.scrollBehavior;
  body.style.scrollBehavior = 'smooth';
  const hasSmooth = getComputedStyle(body).scrollBehavior === 'smooth';
  body.style.scrollBehavior = scrollSave;
  return hasSmooth;
};
 
function SmoothVerticalScrolling(element, time, position) {
  var eTop = element.getBoundingClientRect().top;
  var eAmt = eTop / 100;
  var curTime = 0;
  while (curTime <= time) {
    window.setTimeout(SVS_B, curTime, eAmt, position);
    curTime += time / 100;
  }
}

function SVS_B(eAmt, position) {
  if (position == "center" || position == "")
  window.scrollBy(0, eAmt / 2);
  if (position == "top")
  window.scrollBy(0, eAmt);
}
kr98yfug

kr98yfug8#

另一种可能的解决方案,具有“缓和”效果。

受到前面给出的一些答案的启发,
一个关键的区别是使用“步速”而不是指定持续时间,我发现基于固定步速计算每一步的长度会创建一个平滑的“缓出”效果,因为随着滚动接近目标点,步数会增加。
希望下面的代码易于理解。

function smoothScrollTo(destination) {
    //check if browser supports smooth scroll
    if (window.CSS.supports('scroll-behavior', 'smooth')) {
        window.scrollTo({ top: destination, behavior: 'smooth' });
    } else {
        const pace = 200;
        let prevTimestamp = performance.now();
        let currentPos = window.scrollY;
        // @param: timestamp is a "DOMHightResTimeStamp", check on MDN
        function step(timestamp) {
            let remainingDistance = currentPos < destination ? destination - currentPos : currentPos - destination;
            let stepDuration = timestamp - prevTimestamp;
            let numOfSteps = pace / stepDuration;
            let stepLength = remainingDistance / numOfSteps;

            currentPos = currentPos < destination ? currentPos + stepLength : currentPos - stepLength;
            window.scrollTo({ top: currentPos });
            prevTimestamp = timestamp;

            if (Math.floor(remainingDistance) >= 1) window.requestAnimationFrame(step);
        }
        window.requestAnimationFrame(step);
    }
}

这是我多年来受益于这个伟大的社区后对SO的第一次贡献。建设性的批评是非常赞赏的。

lskq00tm

lskq00tm9#

感谢T.Dayya,我已经结合了关于这个主题的一些答案,这里是带有扩展函数scrollSmoothIntoView的ts模块。

export default {}
    
    declare global {
    
        interface Element {
            scrollSmoothIntoView(): void;
        }
    }
    
    Element.prototype.scrollSmoothIntoView = function()
    {
        const t = 45;
        const tstep = 6.425/t;
        const dummyPoints = new Array(t).fill(0).map((t, i) => circ(i * tstep));
        const dummyPointsSum = dummyPoints.reduce((a, el) => { a += el; return a;}, 0);
    
        const _window: any = window;
        const _elem: any = getScrollParent(this);
    
        const scroll_distance: any = (this as any).offsetTop - (!_elem.parentElement ? _window.scrollY : 0);
    
        let easingPoints = new Array(t).fill(0)
        easingPoints = easingPoints.map((el, i) => {
            return Math.round(scroll_distance * dummyPoints[i] / dummyPointsSum);
        });
    
        const requestAnimationFrame = _window.requestAnimationFrame ||
            _window.mozRequestAnimationFrame ||
            _window.webkitRequestAnimationFrame ||
            _window.msRequestAnimationFrame;
    
        let i = 0;    
        const step = (timestamp:any) => {
            _elem.scrollBy(0, easingPoints[i]);
    
            if (++i < t)
                setTimeout(() => { requestAnimationFrame(step) }, 2);
        };
    
        window.requestAnimationFrame(()=>requestAnimationFrame(step));
    }
    
    function getScrollParent(element: any, includeHidden?: any):any {
        var style = getComputedStyle(element);
        var excludeStaticParent = style.position === "absolute";
        var overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/;
    
        if (style.position === "fixed") return document.body;
        for (var parent = element; (parent = parent.parentElement);) {
            style = getComputedStyle(parent);
            if (excludeStaticParent && style.position === "static") {
                continue;
            }
            if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) return parent;
        }
    
        return document.body;
    }
    
    function circ(t:any) {
        return 1+Math.cos(3+t);
    }

使用html_element.scrollSmoothIntoView()函数。

相关问题