rabin 7 years ago
commit
c28f221d24
2 changed files with 67 additions and 0 deletions
  1. 56 0
      src/events-listen.js
  2. 11 0
      src/total.js

+ 56 - 0
src/events-listen.js

@@ -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);
+        });
+    }
+};

+ 11 - 0
src/total.js

@@ -0,0 +1,11 @@
+if (!total) {
+    var total = true;
+    var refer = encodeURIComponent(document.referrer);
+    var uid = 0;
+    var username = "";
+    var host = location.hostname
+    var tempstr = document.cookie.replace(/(;?)(\s?)session=([^;]*);?/);
+    var session = false;
+    var url = (("https:" == window.location.protocol) ? "https://total.dever.cc/total.php" : "http://total.dever.cc/total.php");
+    document.write('<script language="javascript" type="text/javascript" src="'+url+'?ref='+refer+'&u='+uid+'&n='+username+'&hs='+session+'"></script>');
+}