Skip to content

判断元素与屏幕关系 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bravf opened this issue Nov 30, 2018 · 0 comments
Open

判断元素与屏幕关系 #7

bravf opened this issue Nov 30, 2018 · 0 comments

Comments

@bravf
Copy link
Owner

bravf commented Nov 30, 2018

// 判断一个元素是否在屏幕中
var isInScreen = function ($el, offset){
    var isIn = false

    if (!$el){
        return isIn
    }

    var {top, bottom} = $el.getBoundingClientRect()

    var screenHeight = window.innerHeight
    var screenStart = 0 - offset
    var screenEnd = screenHeight + offset

    if (top < screenStart){
        if (bottom > screenStart){
            isIn = true
        }
    }
    else {
        if (top < screenEnd){
            isIn = true
        }
    }

    return isIn
}
// 判断一个元素是否在屏幕中,并且内容超过半屏
var isHalfScreen = function ($el) {
    var isHalf = false

    if (!$el){
        return isHalf
    }

    var screenHeight = window.innerHeight
    var halfScreenHeight = screenHeight / 2
    var {top, bottom} = $el.getBoundingClientRect()

    if (top < 0){
        if (bottom > halfScreenHeight){
            isHalf = true
        }
    }
    else {
        if ( (top < halfScreenHeight) && (bottom - top > halfScreenHeight) ){
            isHalf = true
        }
    }

    return isHalf
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant