javascript常用代码

高内聚,低耦合。

模块化

//绑定动画结束事件

$('#animationSandbox').removeClass().addClass(x + ' animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
      $(this).removeClass();
    });

//滚动加载

 function bindScroll() {
        window.onscroll = function () {
            var scrollTop = 0;
            var clientHeight = 0;
            var scrollHeight = 0;
            if (document.documentElement && document.documentElement.scrollTop) {
                scrollTop = document.documentElement.scrollTop;
            } else if (document.body) {
                scrollTop = document.body.scrollTop;
            }
            if (document.body.clientHeight && document.documentElement.clientHeight) {
                clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
            } else {
                clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
            }
            scrollHeight = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
            if (scrollTop + clientHeight + 30 > scrollHeight) {
                $("#msg").prepend("===============<br/>");
                window.onscroll = null;
            } else {

            }
        }
    }
View Code

javascript判断数据类型(摘自seajs源码 https://github.com/lifesinger/lifesinger.github.com/issues/175

function isType(type) {
  return function(obj) {
    return Object.prototype.toString.call(obj) === "[object " + type + "]"
  }
}

var isObject = isType("Object")
var isString = isType("String")
var isArray = Array.isArray || isType("Array")
var isFunction = isType("Function")

var _cid = 0
function cid() {
  return _cid++
}
View Code

 //非空判断并赋默认值

seed = (host && host[S]) || {}

//阻止表单默认提交

IE6的onkeypress会接受"回车事件",而onkeydown不会接受 
IE8的onkeypress不会接受"回车事件",而onkeydown会接受

http://hi.baidu.com/wwhhyx/item/298b957bc68450376f29f66c

$("form").bind("keypress keydown",function (e) {
            var event=e||window.event;
            var keycode=event.keyCode|| e.which ||e.charCode;
            if (keycode == 13) {
                return false;
            }
        })

//获取鼠标在当前容器位置

$("#container").bind("mousemove", function (e) {
              var $this = $(this);
              $this.html((e.pageX - $this.offset().left) + "_" + (e.pageY - $this.offset().top))
          })

 // 拖拽、清除文本选择

function _startDrap() {
        $doc.bind('mousemove', _onMsgAreaMouseMove).bind('mouseup', _endDrag);
    }
    function _endDrag() {
        $doc.unbind('mousemove', _onMsgAreaMouseMove).unbind('mouseup', _endDrag);
    }

    // 清除文本选择
    var clsSelect = 'getSelection' in window ? function () {
        window.getSelection().removeAllRanges();
    } : function () {
        try {
            document.selection.empty();
        } catch (e) { };
    };
View Code
原文地址:https://www.cnblogs.com/everyone/p/3208960.html