V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
he2020
V2EX  ›  JavaScript

JavaScript 实现滚动到页面指定位置,这行代码表达了什么?

  •  
  •   he2020 · 2022-01-04 23:26:46 +08:00 · 1346 次点击
    这是一个创建于 835 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原文用 JavaScript 实现了滚动到页面指定位置的功能。
    以下这行代码什么意思?

    destinationOffsetToScroll 似乎是要滚动的距离,但看不出 documentHeight - destinationOffset < windowHeight 这个条件表达了什么。
    
    const destinationOffsetToScroll = Math.round(documentHeight - destinationOffset < windowHeight ? documentHeight - windowHeight : destinationOffset);
    

    原文在: https://pawelgrzybek.com/page-scroll-in-vanilla-javascript/

    xiao109
        1
    xiao109  
       2022-01-05 09:03:14 +08:00
    三元表达式嘛。如果 documentHeight - destinationOffset 小于 windowHeight 就返回 documentHeight - windowHeight ,否则返回 destinationOffset 。
    he2020
        2
    he2020  
    OP
       2022-01-05 12:49:55 +08:00
    @xiao109 知道是三元表达式啊,问题是 documentHeight - destinationOffset < windowHeight 这个判断是什么意思?
    iidear2015
        3
    iidear2015  
       2022-01-05 14:21:30 +08:00
    要滚到的位置是否在视窗内吧
    he2020
        4
    he2020  
    OP
       2022-01-05 14:41:00 +08:00
    @iidear2015 要判断这个做什么
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5255 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:01 · PVG 15:01 · LAX 00:01 · JFK 03:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.