Git Product home page Git Product logo

blog's People

Contributors

shiye515 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

blog's Issues

在没有控制台的浏览器里定位js错误

可以引入各种 console
也可以直接把下面的代码放到head中的script标签里

window.onerror = function(msg, url, lineNo, columnNo, error) {
    alert([
        msg,
        'file: ' + url,
        'lineNo: ' + lineNo,
        'columnNo: ' + columnNo,
        'error: ' + error
    ].join('\n'));
};

怎样触发一个scroll事件

scroll事件的特点

[https://www.w3.org/TR/uievents/#event-types-list] 总结就是:

  • 首先,这个事件属于 UIEvent
  • 这个事件是异步的
  • 大多数情况下没有冒泡阶段(只有在document的触发时才会冒泡到window)
    alt text

创建事件对象

现代浏览器里的推荐做法

使用UIEvent()构造函数

var event = new UIEvent('scroll');

兼容过时浏览器

通过document.createEvent(type)创建,通过event.initUIEvent(type, canBubble, cancelable)初始化

var event = document.createEvent(‘UIEvent’);
event.initUIEvent(‘scroll’, false, true);

如果想在现代浏览器里使用新的构造函数,同时兼容过时浏览器,以下是推荐做法

var event;
try {
    event = new UIEvent('scroll');
} catch (e) {
    event = document.createEvent('UIEvents');
    event.initUIEvent('scroll', false, true);
}

原因是虽然有些过时浏览器有window.UIEvent这个对象,但是做构造函数用时会抛出异常

最后触发事件

element.dispatchEvent(event);

兼容大多数浏览器IE9+,Android2.3+,iOS4.1+

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.