GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [js] 第645天 写一个JS方法,判断元素是否在可视区域 · Issue #3461 · haizlin/fe-interview

[js] 第645天 写一个JS方法,判断元素是否在可视区域 #3461

New issue
Copy link
New issue
Copy link
Open
Open
[js] 第645天 写一个JS方法,判断元素是否在可视区域#3461
Copy link
Labels
jsJavaScript

Description

@haizhilin2013
haizhilin2013
opened on Jan 19, 2021
Issue body actions

第645天 写一个JS方法,判断元素是否在可视区域

3+1官网

我也要出题

Activity

  1. haizhilin2013
    added
    jsJavaScript
    on Jan 19, 2021
  2. xujs0813 commented on Jan 20, 2021

    @xujs0813
    xujs0813
    on Jan 20, 2021
    More actions
    // 判断元素是否在视口中
    function isEleVisible(ele){
        var {top, right, bottom, left} = ele.getBoundingClientRect()
        var w = window.innerWidth
        var h = window.innerHeight
        if(bottom < 0 || top > h){
            // y 轴方向
            return false
        }
        if(right < 0 || left > w){
            // x 轴方向
            return false
        }
        return true
    }
    Reacted by hello087135, I'm Cola., zhanghan and SeanTao Jiang
  3. fanerge commented on Jan 22, 2021

    @fanerge
    fanerge
    on Jan 22, 2021
    More actions

    使用 IntersectionObserver 实现,性能更好
    IntersectionObserver

    // 默认以 viewport 为观察容器
    var intersectionObserver = new IntersectionObserver(function(entries) {
      // todo 如懒加载等
    
    });
    // start observing
    intersectionObserver.observe(document.querySelector('#loadVideo'));
    // 合适的时机停止监听
    intersectionObserver.unobserve(document.querySelector('#loadVideo'))
    Reacted by DiscoverForever and kengojs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.