Skip to content

Commit 71bc0a8

Browse files
committed
Added 'itemchanged' event when changing the active menu item
1 parent cb1ba53 commit 71bc0a8

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

dist/vue-scrollactive.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ exports.default = {
642642
data: function data() {
643643
return {
644644
scrollactiveItems: null,
645-
bezierEasing: __webpack_require__(57)
645+
bezierEasing: __webpack_require__(57),
646+
lastActiveItem: null
646647
};
647648
},
648649

@@ -661,7 +662,8 @@ exports.default = {
661662
methods: {
662663
/**
663664
* Will be called when scrolling event is triggered to handle
664-
* the addition of the active class in the current section item.
665+
* the addition of the active class in the current section item
666+
* and fire the change event.
665667
*/
666668
onScroll: function onScroll() {
667669
var distanceFromTop = window.scrollY;
@@ -697,6 +699,12 @@ exports.default = {
697699
}
698700
}
699701

702+
if (currentItem != this.lastActiveItem) {
703+
// Makes sure to not fire when it's mounted
704+
if (this.lastActiveItem) this.$emit('itemchanged', event, currentItem, this.lastActiveItem);
705+
this.lastActiveItem = currentItem;
706+
}
707+
700708
if (currentItem) currentItem.classList.add(this.activeClass);
701709
},
702710

dist/vue-scrollactive.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/assets/main.js

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,6 @@ exports.default = Plugin;
517517
/* 24 */
518518
/***/ (function(module, exports, __webpack_require__) {
519519

520-
var disposed = false
521520
var Component = __webpack_require__(25)(
522521
/* script */
523522
__webpack_require__(26),
@@ -530,25 +529,6 @@ var Component = __webpack_require__(25)(
530529
/* moduleIdentifier (server only) */
531530
null
532531
)
533-
Component.options.__file = "/Users/mauricio/web/vue-scrollactive/src/scrollactive.vue"
534-
if (Component.esModule && Object.keys(Component.esModule).some(function (key) {return key !== "default" && key.substr(0, 2) !== "__"})) {console.error("named exports are not supported in *.vue files.")}
535-
if (Component.options.functional) {console.error("[vue-loader] scrollactive.vue: functional components are not supported with templates, they should use render functions.")}
536-
537-
/* hot reload */
538-
if (false) {(function () {
539-
var hotAPI = require("vue-hot-reload-api")
540-
hotAPI.install(require("vue"), false)
541-
if (!hotAPI.compatible) return
542-
module.hot.accept()
543-
if (!module.hot.data) {
544-
hotAPI.createRecord("data-v-75a6c496", Component.options)
545-
} else {
546-
hotAPI.reload("data-v-75a6c496", Component.options)
547-
}
548-
module.hot.dispose(function (data) {
549-
disposed = true
550-
})
551-
})()}
552532

553533
module.exports = Component.exports
554534

@@ -756,7 +736,8 @@ exports.default = {
756736
data: function data() {
757737
return {
758738
scrollactiveItems: null,
759-
bezierEasing: __webpack_require__(57)
739+
bezierEasing: __webpack_require__(57),
740+
lastActiveItem: null
760741
};
761742
},
762743

@@ -773,6 +754,11 @@ exports.default = {
773754
},
774755

775756
methods: {
757+
/**
758+
* Will be called when scrolling event is triggered to handle
759+
* the addition of the active class in the current section item
760+
* and fire the change event.
761+
*/
776762
onScroll: function onScroll() {
777763
var distanceFromTop = window.scrollY;
778764
var currentItem = void 0;
@@ -807,8 +793,20 @@ exports.default = {
807793
}
808794
}
809795

796+
if (currentItem != this.lastActiveItem) {
797+
// Makes sure to not fire when it's mounted
798+
if (this.lastActiveItem) this.$emit('itemchanged', event, currentItem, this.lastActiveItem);
799+
this.lastActiveItem = currentItem;
800+
}
801+
810802
if (currentItem) currentItem.classList.add(this.activeClass);
811803
},
804+
805+
806+
/**
807+
* Sets the initial list of menu items, validating if there's none
808+
* or if its hash corresponds to a valid element ID.
809+
*/
812810
setScrollactiveItems: function setScrollactiveItems() {
813811
var scrollactiveItems = document.querySelectorAll('.scrollactive-item');
814812

@@ -845,6 +843,11 @@ exports.default = {
845843

846844
this.scrollactiveItems = scrollactiveItems;
847845
},
846+
847+
848+
/**
849+
* Handles the scrolling when clicking a menu item.
850+
*/
848851
scrollToTargetElement: function scrollToTargetElement(event) {
849852
event.preventDefault();
850853

@@ -1594,20 +1597,13 @@ module.exports = function bezier (mX1, mY1, mX2, mY2) {
15941597

15951598
/***/ }),
15961599
/* 58 */
1597-
/***/ (function(module, exports, __webpack_require__) {
1600+
/***/ (function(module, exports) {
15981601

15991602
module.exports={render:function (){var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;
16001603
return _c('nav', {
16011604
staticClass: "scrollactive-nav"
16021605
}, [_vm._t("default")], 2)
16031606
},staticRenderFns: []}
1604-
module.exports.render._withStripped = true
1605-
if (false) {
1606-
module.hot.accept()
1607-
if (module.hot.data) {
1608-
require("vue-hot-reload-api").rerender("data-v-75a6c496", module.exports)
1609-
}
1610-
}
16111607

16121608
/***/ })
16131609
/******/ ]);

src/scrollactive.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
return {
8989
scrollactiveItems: null,
9090
bezierEasing: require('bezier-easing'),
91+
lastActiveItem: null
9192
}
9293
},
9394
@@ -105,7 +106,8 @@
105106
methods: {
106107
/**
107108
* Will be called when scrolling event is triggered to handle
108-
* the addition of the active class in the current section item.
109+
* the addition of the active class in the current section item
110+
* and fire the change event.
109111
*/
110112
onScroll() {
111113
let distanceFromTop = window.scrollY;
@@ -120,6 +122,12 @@
120122
}
121123
}
122124
125+
if (currentItem != this.lastActiveItem) {
126+
// Makes sure to not fire when it's mounted
127+
if (this.lastActiveItem) this.$emit('itemchanged', event, currentItem, this.lastActiveItem);
128+
this.lastActiveItem = currentItem;
129+
}
130+
123131
if (currentItem) currentItem.classList.add(this.activeClass);
124132
},
125133

0 commit comments

Comments
 (0)