OnZoom

D

Dr J R Stockton

I want to be able to zoom one of my Web pages, by Ctrl-+, Ctrl--, Ctrl-
0, Ctrl-Mousewheel, and to have the top of a given element remain at (or
return to) the top of the screen (without reloading the page).
Preferably for all GUI browsers, or for Firefox 3 & Safari 5, but any of
MSIE 8, Opera 11, Chrome 12 would be of some use.

Rather than the top of an element, the position of the last double-click
would do.

ISTM that there ought to be an onzoom event, and documentation for it.
AFAICS, I was half right; it exists in FF 3.6.18.

But all I've managed to do with the onzoom event is to stop mousewheel
zoom working when the cursor is in a fieldset - any fieldset, even in
another page of the same Firefox window.

N.B. I am NOT using SVG here.

Any suggestions?
 
E

Elegie

On 23/06/2011 00:42, Dr J R Stockton wrote :

Hello John,
I want to be able to zoom one of my Web pages, by Ctrl-+, Ctrl--, Ctrl-
0, Ctrl-Mousewheel, and to have the top of a given element remain at (or
return to) the top of the screen (without reloading the page).
Preferably for all GUI browsers, or for Firefox 3& Safari 5, but any of
MSIE 8, Opera 11, Chrome 12 would be of some use.

Rather than the top of an element, the position of the last double-click
would do.

There are many things to consider here. Firstly, if you want to keep a
box at the same place on the view, then I suggest that you use some
fixed positioning. Note that IE supports fixed positioning only in
standard mode.

You may have noted though, that the box will still move in the view when
you change the zoom, even if it is fixed-positioned. There would be two
ways to adjust for this: a manual way and an automatic way. The manual
way implies to reposition the box on some user action: a double click
event, as you would like, would be a fine way to do it. The automatic
way implies to dynamically reposition the box when a zoom has happened.
ISTM that there ought to be an onzoom event, and documentation for it.
AFAICS, I was half right; it exists in FF 3.6.18.

I am unfortunately not familiar with modern browsers, so I could not
tell whether zoom events, as you have researched, have been defined
consistently cross browsers. However, if you have no event dispatched,
then you may dispatch it yourself, i.e. examine regularly the state of
the view (using setInterval or setTimeout), determine if there is a zoom
(for instance comparing the actual size of an element to an expected
size), and if there is, dispatch the event or take proper action.

The following script should give an illustration of how this could be
performed. I have kept it simple to demonstrate the technique, but you
will have to adjust it to your needs, if you intend to use it. I would
still favor the manual way, though: using intervals like I do below is
not neat, and uses some computer resources.

---
<!DOCTYPE html>
<html>
<head>
<title>Test Zoom</title>
<meta charset="UTF-8">
<style type="text/css">
#info-box {
background-color : yellow;
position : fixed;
top : 10px;
left : 10px;
}
</style>

<script type="text/javascript">
window.onload = (function(evt) {

function setPosition (element, left, top) {
element.style.left = left + "px";
element.style.top = top + "px";
}

function getLeft(element) {
if(!element) return 0;
return element.offsetLeft + getLeft(element.offsetParent)
}

function getTop(element) {
if(!element) return 0;
return element.offsetTop + getTop(element.offsetParent)
}

// Init
if(document.getElementById) {
var info = document.getElementById("info-box");
if(info) {
// Calculate the zoom regularly and reposition accordingly
var interval = setInterval(
(function (originalWidth) {
return (function (){
if (document.body.offsetWidth != originalWidth) {
var zoom = document.body.offsetWidth / originalWidth;
setPosition(info, getLeft(info)*zoom, getTop(info)*zoom);
originalWidth = document.body.offsetWidth;
}
});
})(document.body.offsetWidth),
42
);

// Reposition on double click
document.ondblclick = (function (evt) {
var e=evt||window.event;
setPosition(info, e.clientX, e.clientY);
});
}
}
});
</script>
</head>
<body>
<div id="info-box">Hello, World!</div>
</body>
</html>
 
D

Dr J R Stockton

In comp.lang.javascript message said:
On 23/06/2011 00:42, Dr J R Stockton wrote :

Thanks for your answer.
The ideal solution would be for all browsers to do just that.

I'm still thinking about what you wrote; but in the meanwhile I have
found a sufficiently simple way of achieving sufficiently near to what I
wanted for this occasion - I have put the following code near to the top
of the page <http://www.merlyn.demon.co.uk/www-use1.htm>.

<div onclick="location.href='#ZTO5'"
style="float:left;height:1850ex;width:3px;background:blue;margin-right:1ex;"></div>
<p>&nbsp;<--- Go to somewhere.</p>

That puts a thin blue button along the entire left side of the page, and a
click on it takes me to the current position of interest.
 
E

Elegie

On 04/07/2011 20:31, Dr J R Stockton wrote :

Hi John,
I'm still thinking about what you wrote; but in the meanwhile I have
found a sufficiently simple way of achieving sufficiently near to what I
wanted for this occasion - I have put the following code near to the top
of the page<http://www.merlyn.demon.co.uk/www-use1.htm>.

<div onclick="location.href='#ZTO5'"
style="float:left;height:1850ex;width:3px;background:blue;margin-right:1ex;"></div>
<p>&nbsp;<--- Go to somewhere.</p>

That puts a thin blue button along the entire left side of the page, and a
click on it takes me to the current position of interest.

It seems that I misunderstood what you wanted to achieve. If your
purpose is to have a quick way to navigate a page for development
purposes, then you may like the following:

---
document.onclick = (function(evt) {
if ((evt || window.event).altKey) {
location.href = "#foo";
}
});
---

To activate the navigation, simply click on the document, while pressing
the ALT key. This should be more comfortable than clicking on the blue area.

You can also use regular access keys, although I do not really know how
accessible this can be, given that browsers may reserve some keys for
their own usage.

Regards,
Elegie.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
It seems that I misunderstood what you wanted to achieve.

I think you understood what I really wanted. But your code below is a
much better substitute than my code above. Thanks.
If your purpose is to have a quick way to navigate a page for
development purposes, then you may like the following:

Well, it's really a quick way to stay where I am, in a shifting co-
ordinate system.
---
document.onclick = (function(evt) {
if ((evt || window.event).altKey) {
location.href = "#foo";
}
});
---

To activate the navigation, simply click on the document, while
pressing the ALT key. This should be more comfortable than clicking on
the blue area.

You can also use regular access keys, although I do not really know how
accessible this can be, given that browsers may reserve some keys for
their own usage.


My method is to have one hand located by finger and thumb at the bottom
left edge of the keyboard, for Shift Ctrl Alt, and the other on the
mouse except when entering characters (that helps me type slower than I
think, which is useful). That suits the new code. Others can accept
it.

Your foo, a.k.a. Hither, is defined by
<p ID=Hither>Use Alt-click to return here after Zooming.</p>

Works with right-click too; IE8 then flashes up the context menu for
just long enough to see its size, but FF does not.

I might try to make Alt-click use script moveTo to go to the position of
the last double-click, though I don't yet understand the co-ordinate
system well enough.

But I've just realised that it does not need to be used in IE8, where
the position on the page does not change with Zoom, unlike in Firefox
Opera Safari Chrome.

Query : The above is a GoTo foo, where foo is planted in the source as a
sort of label. Is there a way to GoTo ElemRef, where ElemRef is the
sort of reference given by document.forms[2].lastChild ? IIRC, the
handler for body.ondblclick can get such a reference to the clicked
element.

If you fancy speed-reading old mathematical French to prove that someone
did NOT say something, RSVP. Inversely for c.17 Vatican Latin.
 
E

Elegie

On 07/07/2011 00:43, Dr J R Stockton wrote :

Hi,
I might try to make Alt-click use script moveTo to go to the position of
the last double-click, though I don't yet understand the co-ordinate
system well enough.

Have a look at the following script. Include it on any page to have the
functionality running, without any configuration to perform.

---
(function () {
var scroll = null;

addListener(
document,
"dblclick",
function (evt) { scroll = getScroll(); }
);

addListener(
document,
"click",
function (evt) {
if ((evt || window.event).altKey && scroll) {
window.scrollTo(scroll.x, scroll.y);
}
}
);

function addListener(obj, evt, func) {
if (obj.addEventListener) {
obj.addEventListener(evt, func, false);
} else if (obj.attachEvent) { // Ignoring the "this" value
obj.attachEvent("on"+evt, func);
} else { // Ignoring already attached listeners
obj["on"+evt]=func;
}
}

function getScroll() {
var d = document;
var src = {x:0, y:0};
if (d.documentElement) {
src.x = d.documentElement.scrollLeft || 0;
src.y = d.documentElement.scrollTop || 0;
}
if (document.body) {
if (src.x == 0) src.x = d.body.scrollLeft || 0;
if (src.y == 0) src.y = d.body.scrollTop || 0;
}
return src;
}
})();
---
Query : The above is a GoTo foo, where foo is planted in the source as a
sort of label. Is there a way to GoTo ElemRef, where ElemRef is the
sort of reference given by document.forms[2].lastChild ?

Yes, there would be many ways actually. You could use the
"scrollIntoView" method, which takes an element as argument, and
automatically adjust the scrolling. Or you could also calculate the
absolute position of the element, and use the "scrollTo" method.

However, I believe it should be easier to simply record / restore the
scroll offsets on mouse events, as illustrated by the script above.
IIRC, the
handler for body.ondblclick can get such a reference to the clicked
element.

Indeed. The "target" property of the event object (or the "srcElement"
property of the global window.event object, in IE-like implementations)
holds a reference to the element which was clicked.
If you fancy speed-reading old mathematical French to prove that someone
did NOT say something, RSVP. Inversely for c.17 Vatican Latin.

I am afraid this would be out of my league. The last time I seriously
worked upon mathematical demonstrations would be in my "classes
préparatoires", back in 1997-1998. Also, I happen not being able to read
ancient French (I tried reading Montaigne and Rabelais to no avail;
fortunately, the "Essays" have recently been published in modern French).

Regards,
Elegie.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
, Thu, 7 Jul 2011 16:26:25, Elegie <[email protected]> posted:

I am afraid this would be out of my league. The last time I seriously
worked upon mathematical demonstrations would be in my "classes
préparatoires", back in 1997-1998. Also, I happen not being able to
read ancient French (I tried reading Montaigne and Rabelais to no
avail; fortunately, the "Essays" have recently been published in modern
French).

Ah no, by "old French" I did not mean "Old French", which in current
French would be (?) "l'ancien français" - I meant something much more
recent than those authors - Lagrange, about 1772. The actual question
would have been whether, in his "Essai sur le problème des trois corps"
or elsewhere, he wrote anything about NON-rotating systems.

<http://gallica.bnf.fr/ark:/12148/bpt6k229225j.image.r=Lagrange,+Josep
h-Louis%2C.langEN.f274.pagination> is the well-known part. The French
seems compatible with what I learned in School. I'll just have to start
at p.227 and work through.

Unfortunately, valuable Web sources such as Lagrange and Clavius are in
PDF rather than HTML or plain text.

....

This has been a useful one-sided discussion, because it persuaded me
that I should be able to do it myself sufficiently quickly, by being
careful not to read any formulae. The answer is that he wrote nothing
SPECIFIC about non-rotating systems, but his treatment is sufficiently
general to accommodate the desired case of zero angular velocity.

I deal with it in <http://www.merlyn.demon.co.uk/gravity4.htm>.
 
E

Elegie

On 08/07/2011 23:14, Dr J R Stockton wrote :

Hi,
Ah no, by "old French" I did not mean "Old French", which in current
French would be (?) "l'ancien français" - I meant something much more
recent than those authors - Lagrange, about 1772. The actual question
would have been whether, in his "Essai sur le problème des trois corps"
or elsewhere, he wrote anything about NON-rotating systems.

<http://gallica.bnf.fr/ark:/12148/bpt6k229225j.image.r=Lagrange,+Josep
h-Louis%2C.langEN.f274.pagination> is the well-known part. The French
seems compatible with what I learned in School. I'll just have to start
at p.227 and work through.

I read it; the French used by the author is the same as modern French,
except for one or two expressions, a bit old. I have found nothing about
non-rotating systems - AIUI he speaks about bodies in a general way, and
the only explicit assumption being made in the article is the mass of
the bodies being positive.
This has been a useful one-sided discussion, because it persuaded me
that I should be able to do it myself sufficiently quickly, by being
careful not to read any formulae. The answer is that he wrote nothing
SPECIFIC about non-rotating systems, but his treatment is sufficiently
general to accommodate the desired case of zero angular velocity.

I love when I can help without doing anything :)

Regards,
Elegie.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,110
Latest member
OdetteGabb
Top