开发者

jQuery.bind and .trigger without jQuery

开发者 https://www.devze.com 2023-04-09 04:58 出处:网络
I want to do the equivalent of this: $(window).bind(\'resize\', function () { myResizeFunction(); }).trigger(\'resize\');

I want to do the equivalent of this:

$(window).bind('resize', function () {
    myResizeFunction();
}).trigger('resize');

without jquery.

The reason being is. This is the only part of code in my javascript library that uses jquery.

And i see no point in includin开发者_C百科g jquery if im only using this one command.

NOTE: Just to clarify, i want myResizeFunction to run everytime the broswer is resized. A cross-browser solution is prefered!

Thanks:)


Simply use this

<script>
var resizeMe = function() {
    myResizeFunction();
}

resizeMe();
</script>

To hook with resize event of window

window.onresize = resizeMe


As far as I know, jQuery is simply using window.onresize.

window.onresize = function() {
    //stuff
};

For your case:

window.onresize = myResizeFunction;
myResizeFunction();


// binding event
if(window.addEventListener)window.addEventListener("resize",myResizeFunction,false);
else if(window.attachEvent)window.attachEvent("onresize",myResizeFunction);
// trigger event handler
myResizeFunction();

It's not full equivalent of jQuery .bind and .trigger, but it's valid main usage of resize event

0

精彩评论

暂无评论...
验证码 换一张
取 消