Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  90] [ 7]  / answers: 1 / hits: 28884  / 13 Years ago, thu, may 26, 2011, 12:00:00

I am using jQuery plugin for resizing the elements. Elements height & width is set in percentage (%) not in pixel. Whenever i re-size any element it's height & width changes to pixels which is fixed but i wanted this change in percentage.



I am sure there must be some technical issue for this but there would be solution too.



#parentDiv { height:100%; width:100%;}

.image { height: 25%; width: 25%; }

<body>
<div id = parentDiv>
<div class=image id=image1 onmouseover=resizeMe(this);> </div>
<div class=image id=image2 onmouseover=resizeMe(this);> </div>
</div>

<script>
function resizeMe(obj) {
$(#obj.id).resizable(); }

</body>


But once i re-size id=image1, below is HTML code. it contains the height & width size in pixels. Does anybody light on it, How can i manage to re-size in percentage only.



<div class=image style=width: 345px; height: 52px;>

More From » jquery

 Answers
9

jQuery gets its values from the browsers DOM engine, which always return normalized values in pixels no matter how they were intially set.



So the only way you will be able to do this is by recalculating the percentages based on elements width/height and parents width/height once the resizing has stopped.



$(.image).resizable({
autoHide: true,
stop: function(e, ui) {
var parent = ui.element.parent();
ui.element.css({
width: ui.element.width()/parent.width()*100+%,
height: ui.element.height()/parent.height()*100+%
});
}
});


See the above in action on jsFiddle



Notice how I'm setting the width/height with .css instead of using .width and .height. This is because the latter will be normalized by the DOM engine and turned into pixels.



Now to some feedback on your code. It's quite a mess to be honest. It's a really bad practice to use inline styling and scripts. Set the width/height of #parentDiv with css just as you did with .image. And don't use onmouseover attribute. Instead bind the event with jQuery using .bind.



Also you shouldn't make the elements resizable on mouse over. It should be done once, when the document is ready. Use the autoHide option to hide the handlers while the mouse isn't over the element. An alternative is to bind enable and disable methods to mouse hover, but that will not have the same result.


[#92044] Tuesday, May 24, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aidan

Total Points: 72
Total Questions: 95
Total Answers: 121

Location: Uzbekistan
Member since Sat, Feb 27, 2021
3 Years ago
aidan questions
Mon, Oct 11, 21, 00:00, 3 Years ago
Wed, Sep 29, 21, 00:00, 3 Years ago
Sun, Sep 5, 21, 00:00, 3 Years ago
Thu, Jan 16, 20, 00:00, 4 Years ago
;