What You'll Be Creating |
JavaScript is the last stop of this draggable menu journey, but before we write one line of JS we’ll need to write a module pattern setup.
- var dragaebelMenu = (function() {
- function doSomething() {…}
- return {
- init: function() {…}
- }
- })();
- dragaebelMenu.init(); // start it!
For the configuration setup we’ll define some variables for future reference.
- var dragaebelMenu = (function() {
- var container = document.querySelectorAll('.js-dragsurface')[0],
- nav = document.querySelectorAll('.js-dragnav')[0],
- nav_trigger = document.querySelectorAll('.js-dragtoggle')[0],
- logo = document.querySelectorAll('.js-draglogo')[0],
- gs_targets = [ container, nav, logo, nav_trigger ],
- closed_nav = nav.offsetWidth + getScrollBarWidth();
- })();
Methods
To keep things short I’ll only discuss methods that are extremely important to the functionality of the menu behavior. Everything else that you’ll see in the demo not discussed here is the “sugar on top” stuff that makes the menu even more powerful.
- function menu(duration) {
- container._gsTransform.x === -closed_nav ?
- TweenMax.to(gs_targets, duration, { x: 0, ease: Linear.easeIn }) :
- TweenMax.to(gs_targets, duration, { x: -closed_nav, ease: Linear.easeOut });
- }
- function isOpen() {
- return container._gsTransform.x < 0;
- }
- function updateNav(event) {
- TweenMax.set([nav, logo, nav_trigger], { x: container._gsTransform.x });
- }
- function enableSelect() {
- container.onselectstart = null; // Fires when the object is being selected.
- TweenMax.set(container, { userSelect: 'text' });
- }
- function disableSelect() {
- TweenMax.set(container, { userSelect: 'none' });
- }
- function isSelecting() {
- // window.getSelection: Returns a Selection object representing
- // the range of text selected by the user or the current position
- // of the caret.
- return !!window.getSelection().toString().length;
- }
Draggable Instance
- Draggable.create([targets], {options})
- Draggable.create([container], {
- type: 'x',
- dragClickables: false,
- throwProps: true,
- dragResistance: 0.025,
- edgeResistance: 0.99999999,
- maxDuration: 0.25,
- throwResistance: 2000,
- cursor: 'resize',
- allowEventDefault: true,
- bounds: {…},
- onDrag: updateNav,
- onDragEnd: function(event) {…},
- liveSnap: function(value) {…},
- onPress: function(event) {…},
- onClick: function(event) {…},
- onThrowUpdate: function() {…}
- });
Written by Dennis Gaebel
If you found this post interesting, follow and support us.
Suggest for you:
JavaScript For Beginners - Learn JavaScript From Scratch
JavaScript for Absolute Beginners
JavaScript For Absolute Beginners - Build Simple Projects
JavaScript Bootcamp - 2016
No comments:
Post a Comment