|
@@ -0,0 +1,56 @@
|
|
|
+
|
|
|
+ * 示例:
|
|
|
+ new cnaEvent($('.artlove'), [{
|
|
|
+ event: 'scroll',
|
|
|
+ eventCategory: "eventCategory",
|
|
|
+ eventAction: "eventAction",
|
|
|
+ eventLabel: "eventLabel"
|
|
|
+ }, {
|
|
|
+ event: 'click',
|
|
|
+ eventCategory: function(dom) {
|
|
|
+ return dom.text() + "eventCategory" },
|
|
|
+ eventAction: function(dom) {
|
|
|
+ return dom.text() + "eventAction" },
|
|
|
+ eventLabel: function(dom) {
|
|
|
+ return dom.text() + "eventLabel" }
|
|
|
+ }])
|
|
|
+ */
|
|
|
+function cnaEvent(el, events) {
|
|
|
+ this.el = el;
|
|
|
+ if(!this.el || !this.el.length){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.events = events;
|
|
|
+ this.bind();
|
|
|
+}
|
|
|
+cnaEvent.prototype = {
|
|
|
+ bind: function() {
|
|
|
+ var that = this,
|
|
|
+ elTop = this.el.offset().top,
|
|
|
+ wh = $(window).height(),
|
|
|
+ rid = "I" + new Date().getTime() + Math.floor(Math.random() * 1e5);
|
|
|
+ $(this.events).each(function(i, item) {
|
|
|
+ (function(ev, eventCategory, eventAction, eventLabel) {
|
|
|
+ if (ev == "scroll") {
|
|
|
+ $(window).on("scroll." + rid, function() {
|
|
|
+ var top = $(this).scrollTop();
|
|
|
+ var ec = typeof eventCategory === "function" ? eventCategory($(this)) : eventCategory,
|
|
|
+ ea = typeof eventAction === "function" ? eventAction($(this)) : eventAction,
|
|
|
+ el = typeof eventLabel === "function" ? eventLabel($(this)) : eventLabel;
|
|
|
+ if (top + wh > elTop) {
|
|
|
+ $(window).off("scroll." + rid);
|
|
|
+ cna('send', 'event', ec, ea, el);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ that.el.on(ev, 'a,input,button', function() {
|
|
|
+ var ec = typeof eventCategory === "function" ? eventCategory($(this)) : eventCategory,
|
|
|
+ ea = typeof eventAction === "function" ? eventAction($(this)) : eventAction,
|
|
|
+ el = typeof eventLabel === "function" ? eventLabel($(this)) : eventLabel;
|
|
|
+ cna('send', 'event', ec, ea, el);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })(item.event, item.eventCategory, item.eventAction, item.eventLabel);
|
|
|
+ });
|
|
|
+ }
|
|
|
+};
|