-1

here i am posting a jquery code which is working but few lines are not clear to me . js fiddle link.

full code

HTML:

<form method="post" action="WebForm1.aspx" id="ctl00">
    <input type="submit" name="Button1" value="Fly DIV" id="Button1" class="toggleUPSContainer" />
</form> 

CSS:

#UPSContainer{
    position: absolute;
    display: none;
    background:red;
    height:0;
    width:0;
}

javascript:

$(document).ready(function () {
    var $els = [];
    var data = {
        "UPSContainer": {
            "height" : 100,
            "width" : 100
        },
        "isAnimating" : false
    };
    $els.window = $(window);
    $els.form = $('#ctl00');
    $els.toggleUPSButtons = $('.toggleUPSContainer');

    function addUPSOverlay(){
        $els.form.append('<div id="UPSContainer"></div>');
        $els.UPSContainer = $('#UPSContainer');
    }

    function getNewWindowCorner(){
        data.windowWidth = parseInt($els.window.width());
        data.windowHeight = parseInt($els.window.height());
        if($els.UPSContainer.is(':hidden')){
            $els.UPSContainer.css({
                top: data.windowHeight + 'px',
                left: data.windowWidth + 'px'
            });
        }else{
            $els.UPSContainer.css({
                left: ((data.windowWidth - data.UPSContainer.width) / 2) + 'px',
                top: ((data.windowHeight - data.UPSContainer.height) / 2) + 'px'
            });
        }
    }

    function containerOpenComplete(){
        // do what you want here when opening complete
    }

    function containerCloseComplete(){
        // do what you want here when closing complete
    }

    function toggleUPSOverlay(e){
        e.preventDefault();
        if(!data.isAnimating){
            if($els.UPSContainer.is(':hidden')){ // currently closed so open
                $els.UPSContainer.show();
                $els.UPSContainer.animate({
                        left: ((data.windowWidth - data.UPSContainer.width) / 2) + 'px',
                        top: ((data.windowHeight - data.UPSContainer.height) / 2) + 'px',
                        height: data.UPSContainer.height + 'px', 
                        width: data.UPSContainer.width + 'px'
                }, 200, function(){
                    containerOpenComplete();
                });
            }else{ // currently open so close
                $els.UPSContainer.animate({
                        left: data.windowWidth + 'px',
                        top: data.windowHeight + 'px',
                        height: 0 + 'px', 
                        width: 0 + 'px'
                    }, 200,
                    function () {
                        $els.UPSContainer.hide();
                        containerCloseComplete();
                    });
            }
        }
    }

    function attachEvents(){
        $els.window.on('resize', getNewWindowCorner);
        $els.toggleUPSButtons.on('click', toggleUPSOverlay);
    }

    function initialize(){
        addUPSOverlay();
        getNewWindowCorner();
        attachEvents();
    }

    initialize();
});

this function's code is not clear

function getNewWindowCorner(){
        data.windowWidth = parseInt($els.window.width());
        data.windowHeight = parseInt($els.window.height());
    }

just see windowWidth & windowHeight potion

var data = {
        "UPSContainer": {
            "height" : 100,
            "width" : 100
        },
        "isAnimating" : false
    };

data has been declared as object and it has property like height, width & isAnimating etc but how we can add two new property called windowWidth & windowHeight to data object ??

just seeing the below code it seems that we can add any property to any object with any name at runtime in javascript and here we are storing value to windowWidth & windowHeight property.

data.windowWidth = parseInt($els.window.width());
data.windowHeight = parseInt($els.window.height());

so just see my whole code and tell me from where these two property comes windowWidth & windowHeight ? thanks

EDIT 1

Need bit more help. what it is called in js var $els = [];? is it array or object? what is the difference between var $els = []; and var els = {}; ? if this is array var $els = []; then how one can push anything into array like this way $els.window = $(window); or $els.body = $('body');. please help me understand the usage with small sample. thanks

3 Answers 3

1

For normal ones you can add them on the fly. They don't have to be declared from the start with the object. If they are they'll only be overwritten.

So you could either add everything you want from the start...

var data = {
  "UPSContainer": {
    "height": 100,
    "width": 100
  },
  "isAnimating": false
};

.. or create it as you go

var data = {};

data.UPSContainer = {};

data.UPSContainer.height = 100;
data.UPSContainer.width = 100;

data.isAnimating = false;

data.windowWidth = parseInt($els.window.width());
data.windowHeight = parseInt($els.window.height());
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for nice reply. please see my edit 1 because i need few more help.
1

it seems that we can add any property to any object with any name at runtime in javascript

Yes. You can. You just assign the new values.

where these two property comes windowWidth & windowHeight

From the code you are asking about. The assignment creates those properties.

Comments

1

In ECMAscript3 and ECMAScript5 , any property can be added to any object, either by dot assignment, or through an array type notation.

data.windowWidth = parseInt($els.window.width());

or

data['windowWidth'] = parseInt($els.window.width());

create 'windowWidth' property for the data object. In ECMA5 , this behaviour can be altered by following function:

Object.preventExtensions( obj )

and you can use Object.isExtensible( obj ) to check the current behaviour of that object. If you try adding property to such an object for which extensions have been prevented, you would get an error in strict mode.

For your second question, in javascript array is also an object having properties , date is also an object , hence you can add properties to the array.

1 Comment

thanks for nice reply. please see my edit 1 because i need few more help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.