原文用JavaScript实现了滚动到页面指定位置的功能。
以下这行代码什么意思?destinationOffsetToScroll
似乎是要滚动的距离,但看不出documentHeight - destinationOffset < windowHeight
这个条件表达了什么。
const destinationOffsetToScroll = Math.round(documentHeight - destinationOffset < windowHeight ? documentHeight - windowHeight : destinationOffset);
documentHeight
是整个文档的高度,destinationOffset
是最后想要滚动到的位置,windowHeight
是屏幕高度。documentHeight - destinationOffset < windowHeight
意思是目标位置在当前屏幕内(滚动的剩余距离小于1屏)时,再滚动剩余距离;大于1屏时再滚动1屏的距离。