Skip to content

Instantly share code, notes, and snippets.

@corypina
Created November 3, 2022 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corypina/8a732660fe8db46e525c9c41ff2e5b82 to your computer and use it in GitHub Desktop.
Save corypina/8a732660fe8db46e525c9c41ff2e5b82 to your computer and use it in GitHub Desktop.
Move an element from one place to another based on browser/device width
let breakPoint = 768
let moveThis = document.querySelector('.nodeToMove')
let newParent = document.querySelector('.newParentNode')
let origin = document.querySelector('.originalParentNode')
const moveTheNode = () => {
let viewportWidth = window.innerWidth || document.documentElement.clientWidth;
if (viewportWidth < breakPoint) {
newParent.after(moveThis)
} else {
origin.prepend(moveThis)
}
}
window.addEventListener('resize', moveTheNode);
window.onload = moveTheNode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment