zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    app卡顿问题检测--KMCGeigerCounter
    报错---[UIApplication _runWithMainScene:transitionContext:completion:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3505.16/UIApplication.m:3294**
    键盘工具栏的快速集成--IQKeyboardManager
    iOS 对网络视频采集视频截图
    iOS-label出现未知边框线的bug
    iOS开发中图片方向的获取与更改
    通过代码设置button中文字的对齐方式
    util.date
    统计字符串每个字母的个数
    异常处理之多重catch
  • 原文地址:https://www.cnblogs.com/everyone/p/3208960.html
Copyright © 2011-2022 走看看