Created
May 26, 2017 17:20
-
-
Save dblodorn/c5f8d142cc2a3210d78f6f7b3acded7b to your computer and use it in GitHub Desktop.
width-check.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import widthCheck from './_app/width-check' | |
// Test Functions | |
const testFunc1 = () => { | |
console.log('test func 1') | |
} | |
const testFunc2 = () => { | |
console.log('test func 2') | |
} | |
widthCheck({ | |
breakpoint: 650, | |
aboveFunction: testFunc1, | |
belowFunction: testFunc2 | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import throttle from 'lodash/throttle' | |
import mixin from 'lodash/mixin' | |
import _ from 'lodash/wrapperLodash' | |
mixin(_, { throttle: throttle }) | |
export default function widthCheck (config) { | |
let isSmall = true | |
const af = config.aboveFunction.toString() | |
const bf = config.belowFunction.toString() | |
const wwC = (w, af, bf) => { | |
let ww = window.innerWidth | |
if (ww >= w && isSmall) { | |
const asFuncAf = eval('(' + af + ')') | |
asFuncAf() | |
isSmall = false | |
} else if (ww < w && !isSmall) { | |
const asFuncBf = eval('(' + bf + ')') | |
asFuncBf() | |
isSmall = true | |
} | |
} | |
wwC(config.breakpoint, af, bf) | |
window.addEventListener('resize', _.throttle(function () { | |
wwC(config.breakpoint, af, bf) | |
}, 50)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment