推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
he2020

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

  •  
  •   he2020 · Jan 4, 2022 · 2515 views
    This topic created in 1773 days ago, the information mentioned may be changed or developed.

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

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

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

    xiao109
    xiao109
    Jan 5, 2022
    1
    三元表达式嘛。如果 documentHeight - destinationOffset 小于 windowHeight 就返回 documentHeight - windowHeight ,否则返回 destinationOffset 。
    he2020
    he2020
    OP
    Jan 5, 2022
    2
    @xiao109 知道是三元表达式啊,问题是 documentHeight - destinationOffset < windowHeight 这个判断是什么意思?
    iidear2015
    iidear2015
    Jan 5, 2022
    3
    要滚到的位置是否在视窗内吧
    he2020
    he2020
    OP
    Jan 5, 2022
    4
    @iidear2015 要判断这个做什么
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2125 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 12:27 · PVG 20:27 · LAX 04:27 · JFK 07:27
    ♥ Do have faith in what you're doing.