简体   繁体   中英

rewrite native javascript in jQuery

I've got a native JavaScript function and I'd like to rewrite it in jQuery. Can you help me? Way beyond my understanding...

EDIT: Quickly addressing the "please refactor my code for me" comments- I'm not asking you to rewrite the work, I'd simply like some guidance on how to rewrite this. As you all have experienced at some point, documentation, tutorials, and books that are helpful can be difficult to find. Any advice is welcome, but..

if(you dont have anything nice to say){
    dont
}



 var RESIZER = function(){ 

    this.prevWidth = resizee.offsetWidth;
    this.prevHeight = resizee.offsetHeight;

    this.resizee = resizeeContainer.getElementsByTagName('video')[0];
    this.resizeeContainer = resizee.parentNode;
    this.resizeeStyle = this.resizee.style;

    var ratio = this.resizee.offsetHeight/this.resizee.offsetWidth;

    var that = this;

    this.Init = function(){
        if( that.resizeeContainer.className.lastIndexOf("resizerd")>=0 )
        {
            var resizeeContOffsetWidth = that.resizeeContainer.offsetWidth;
            var resizeeOffsetWidth = that.resizee.offsetWidth;
            var resizeeContOffsetHeight = that.resizeeContainer.offsetHeight;
            var resizeeOffsetHeight = that.resizee.offsetHeight;

            if(that.prevWidth!= resizeeContOffsetWidth)
            {
                that.prevWidth = resizeeContOffsetWidth;
                var desired = resizeeContainer.offsetHeight/resizeeContainer.offsetWidth;
                if(desired>ratio){
                    that.resizeeStyle.width=resizeeContOffsetWidth*desired+resizeeContOffsetWidth*desired+"px";
                    that.resizeeStyle.left = -1*(resizeeOffsetWidth-resizeeContOffsetWidth)/2+'px';
                }
                else{ 
                 that.resizeeStyle.cssText="width:100%;height:auto;position:fixed;";
                }
            }

            if(that.prevHeight!=resizeeContOffsetHeight)
            { 
                that.prevHeight = resizeeContOffsetHeight;
                var desired = resizeeContOffsetHeight/resizeeContOffsetWidth;  
                if(desired>ratio){  console.log(ratio);
                    //that.resizeeStyle.top = '0px';
                    that.resizeeStyle.left = -1*(resizeeOffsetWidth-resizeeContOffsetWidth)/2+'px';
                    that.resizeeStyle.width = resizeeContOffsetHeight*desired+resizeeContOffsetHeight/desired+'px';
                }
                else
                {
                    that.resizeeStyle.top = -1*(resizeeOffsetHeight-resizeeContOffsetHeight)/2+'px';

                }
            }

        }
    };
};

var myResizerObject = new RESIZER();
window.onresize = myResizerObject.Init;

Developing jQuery plugins, while not real difficult, is not trivial either. The only way to do it is to start with a tutorial and learn the ins and outs. Then you can take a piece of code and turn that code into a functioning plugin. But you have to learn some things first.

If you haven't gone through the jQuery beginner plugin tutorial, I would: http://docs.jquery.com/Plugins/Authoring Try developing a simple plugin first, like an input field validation plugin or something.

I'm not sure what you mean by the native script and browsers interpreting differently; some JS is browser-dependent. jQuery-native functions should be browser-agnostic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM