How to check if an element is in the view of the user with jquery

前端 未结 6 753
孤街浪徒
孤街浪徒 2020-11-30 04:28

I have a very big draggable div in my window. This div has a smaller window.

6条回答
  •  天涯浪人
    2020-11-30 04:49

    My solution is using the given code example, and it will show you an overall idea of how to determine whether the li element is visible. Check out the jsFiddle which contains code from your question.

    The jQuery .offset() method allows us to retrieve the current position of an element relative to the document. If you click on an li element inside the draggable, your offset from the top will be between 0 and 500 and the offset from the left should be between 0 and 500. If you call the offset function of an item that is not currently visible, the offset will either be less than 0 or greater than 500 from either the top or left offset.

    If its not a daunting task I always like to code what I need from 'scrath' it gives me more flexibility when having to modify or debug, hence why I would recommend looking into using jQuery's offset function instead of using a plugin. If what you are trying to accomplish is fairly simple, using your own function will give you one less library to load.

提交回复
热议问题