crashgordon Posted October 16, 2009 Share Posted October 16, 2009 Hello I must explain im a completely new to Java script and programming and im finding it hard to understand the way some variables can work im trying this below this but it doest seem work, what, im i doin wrong? please can any body give me some hints I have looked on-line for example but cant seem to make things fit together the variable takes values from dhtml objects that move so below im trying to say if the image display.y is less than or equal to the croparea .y then its true else its false var T_hit = "im_dis.y <= cropArea.y" function findImageEdge() { if (T_hit == true) { TPos.setOpacity(1) } else if (T_hit != false) { TPos.setOpacity(0.5) } } many thanks Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted October 16, 2009 Share Posted October 16, 2009 Quotes (single and double) denote strings, or groups of characters that are supposed to be processed as-is. T_hit isn't true or false because of this...it is literally im_dis.y <= cropArea.y. You should have: var T_hit = im_dis.y <= cropArea.y; function findImageEdge() { if(T_hit == true) { TPos.setOpacity(1); } else { TPos.setOpacity(0.5); } } An even simpler version would be: function findImageEdge() { if(im_dis.y <= cropArea.y) { TPos.setOpacity(1); } else { TPos.setOpacity(0.5); } } Quote Link to comment Share on other sites More sharing options...
crashgordon Posted October 16, 2009 Author Share Posted October 16, 2009 many thanks Nightslyr, i started out with if(im_dis.y <= cropArea.y) { TPos.setOpacity(1); and it worked well but im thinking that if i used the test in another part function it would be better to make it into a variable. Make it run faster or smoother? I really have no idea of the best way, the function is much bigger than i have posted and tests for the position of a draggable image relative to the crop area around it, with top. left right and bottom, if a certain state is activated the way the image moves to compensate the default scaling form the top left corner changes so for example if the top hit test is false and the right hit test is false then the image when scaled down shrinks back to the top right corner buy using a move to function. the problem im having is that a big funtion that test for all theses is not very robust so I thought variables would make it work better?, unless there is a better way to test for these? have ya any ideas? onac again many thanks for the input so far Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted October 16, 2009 Share Posted October 16, 2009 many thanks Nightslyr, i started out with if(im_dis.y <= cropArea.y) { TPos.setOpacity(1); and it worked well but im thinking that if i used the test in another part function it would be better to make it into a variable. Make it run faster or smoother? I really have no idea of the best way, the function is much bigger than i have posted and tests for the position of a draggable image relative to the crop area around it, with top. left right and bottom, if a certain state is activated the way the image moves to compensate the default scaling form the top left corner changes so for example if the top hit test is false and the right hit test is false then the image when scaled down shrinks back to the top right corner buy using a move to function. the problem im having is that a big funtion that test for all theses is not very robust so I thought variables would make it work better?, unless there is a better way to test for these? have ya any ideas? onac again many thanks for the input so far Well, stuffing things into variables can be more efficient in some cases, the most frequent being when you need to grab a hold of an HTML element. A lot of people code in the style of: function someFunc() { var myVar = document.getElementById('someElem').value + 5; var myVar2 = document.getElementById('someElem').style.border; if(myVar2 == "1px solid black") { document.getElementById('someElem').style.border = ""; } } Multiple document calls to find the same element, which is horribly inefficient. So, using variables wisely will make the code better overall: function someFunc() { var elem = document.getElementById('someElem'); var newValue = elem.value + 5; var elemBorder = elem.style.border; if(elemBorder == "1px solid black") { elemBorder = ""; } } Why is this more efficient? It's due to the fact that JavaScript makes heavy use of references, and it's far more efficient to put a reference to an element in a variable (elem, in this case) and manipulate it through that reference than it is to tell JavaScript to search through the HTML document and retrieve the same reference repeatedly. In other words, my second example essentially caches the result of the document search, and uses that to work. So, with that canned example out of the way, I can't really comment on your code specifically without seeing it. Quote Link to comment Share on other sites More sharing options...
crashgordon Posted October 19, 2009 Author Share Posted October 19, 2009 Thanks for the tips Nightslyr, one of the problems i think im having is that the variables are dynamic and because Im only calling them on load it looks like they dont work, Im using an API that has a function to keep updating ondrag, is the way to go to call the variables in the ondrag function? is this allowed? im getting closer to sorting this out but am away from today so please don think me ignorant if I don't reply for a few days and once again thanks for the help so far, im getting more of an idea about these now Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.