Skip to content

micro-js/offset-parent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

offset-parent

Build status Git tag NPM version Code style

Find the offset parent of a DOM element

Installation

$ npm install @f/offset-parent

Usage

The offset parent of a given element is the first parent up the tree that is positioned absolute, relative, or fixed.

var offsetParent = require('@f/offset-parent')

function position (node, placement) {
  var parent = offsetParent(node)

  node.style.left = (parent.offsetWidth / 2) - (node.offsetWidth / 2) + 'px'
  switch (placement) {
    case 'top':
      node.style.top = -node.offsetHeight + 'px'
      break
    case 'bottom':
      node.style.bottom = '0px'
      break
  }
}

API

offsetParent(node)

  • node - The DOM node who's offsetParent you want to find

Returns: The first node above node that is positioned absolute, relative, or fixed.

License

MIT