I’ve modified your code, so you can see, that your example doesn’t work when setting myDiv2.style.width.
IE returns all decimals when not used to set object’s style width/height/top/left etc.
And herein lies your answer: find a strategy to round on 2 decimals (the common denominator) all over the place by yourself and forget about browser diffs when doing numbers from JS to CSS.
Or… if you need exact calculation to translate to sane pixel values, use relative values, e.g.:
However, as it was mentioned above, this isn’t normally a problem, and significance for subpixel values of up to so many decimals usually indicates a wrong approach.
@Pullo, I’m working on a zoom script and currently developing variable zoom, which will allow users to zoom In even closer using mousewheel. Something like this http://www.elevateweb.co.uk/image-zoom/examples#mousewheel
Opera also returns from one to two decimals, but it makes correct roundings. IE returns almost the same numbers, but sometimes they may vary by 0.01px.
You can see from the example above that when using mousewheel the size of the frame on the image is decreased proportionately and the size of the image to the right is being increased simultaneously. In my script the image to the right let’s say is multiplied by 5, so if the initial size of the image was 200x200 pixels, it will become 1000x1000 pixels in order to be displayed correctly.
When the user uses mousewheel I’m adding, for example, 0.205 to the 5 which increases the size of the image to the right. And I also decrease the size of the frame on the image by say 7.88787 pixels, to ensure that the correct part of the increased image is displayed.
It’s quite difficult to explain, so I hope that the link above will help you to better understand what I’m trying to say and do.
When it comes to browsers, they’ll just do their own thing. That’s why you can’t trust their rounding and passed number values, you have to handle the rounding yourself.
Take the example bellow. After rounding, the calculated width displayed in “style” attribute is 135.212px, yet the browser displays a computed 135.217px. Use Firebug or any other inspector.
|>width: 135.217px;
--this.style 135.212px
And the features you’re describing here, zoom-in, zoom-out, subpixel values belong to SVG and canvas, not image frames.
Your suspicion is wrong @myty. I don’t use third party libraries and only code on javascript. By doing so I learn things which I would have never learned if I used other libraries/frameworks.
Besides, 3rd party libs don’t always provide the capacity one sometimes needs to create new animations/plugins etc.
There are also cases when say some plugin written on jQuery won’t work when jQuery’s library is updated. Thus relying on your own code written on javascript is more secure than using other libraries.
Regarding @Pullo’s, @Rubble’s question (and those who’re interested) on how I overcame my problem.
I decided to calculate my div’s width and height on every mousewheel event. For instance, If I allow user to scroll 11 times to and fro, the script now calculates what the width of the div will be when scrolled say 9 times forward. And every time I return the exact number I need for every browser.
So, if I’ve scrolled say 8 times the script checks, if it is the 8th mousewheel event it returns the number needed for that event.
I hope my explanation makes sense.
PS. Sorry for any mistakes in the text, as English isn’t my first language.
I’m sorry. I wanted to say not “ON every mousewheel event” but “FOR every mousewheel event”!
And the script makes calculations before the user starts scrolling, so I have all the numbers beforehand for every mousewheel event.
You were really hung up on getting exact numbers in IE. You said that in your case those decimals make difference. And you said you’ve found a solution to your problem.
It would help best for you to be clear in your explanation: did you find a way to coerce IE into submission to accept more than two decimals?
I know the answer, but others may be still looking for an answer in all the wrong places.
The reason I’m posting this is not to create animosity. I believe it’s better for one to follow path on best practices. Believe it or not, libraries often do follow path on best practices. You should not dismiss proven and tested code. Even if you are JavaScript educated.
I’m not saying they don’t and I’m not dismissing proven or tested code. And of course, best libraries do follow path on best practices, otherwise they won’t be among the best.
I said that those decimals did make difference and I wanted to find a way to make IE return those decimals, but as I’ve found out it’s not possible, at least for now.
I just wanted to find a way to finish my project. And I finally did it by “circumventing” this problem and solving it in another way.
I do understand that you were/are indicating my wrong approach on solving this problem. Maybe you are correct that my whole approach regarding variable zoom was “wrong”, but I managed to find a way to solve this problem - a thing I was aiming for.
But, anyway, thanks for your replies, they were helpful as well.