-4

I have a number of images in my HTML document.

The user triggers some changes (AJAX calls) when clicking on an image or by pressing a key.

To keep track of the latest image (client-side), I use javascript and onmouseover, assigning the image ID to a javascript-variable which in turn is used to fill the AJAX calls.

Everything works well (even it there might be better ways to do it), but sometimes it takes some time to refresh the image. For a moment, it ceases to exist, causing the image next to it to jump left.

This, of course, moves that image underneath the pointer, triggering the mouseover event.

How(?) is it possible to distinguish between a mouseover event caused by mouse-movement from one fired by a layout change?

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
Gyro Gearloose
  • 1,056
  • 1
  • 9
  • 26
  • I edited the title and the text to try and make it clearer, feel free to roll back if you don't like it. – Pekka Feb 28 '16 at 20:28
  • @Pekka웃 thank you, an ecample code is here: http://stackoverflow.com/q/35231924/4142984 the composite described there is placed a few times horizontally into what results as a div-tag in html. – Gyro Gearloose Feb 28 '16 at 20:35
  • The short answer is you can't really tell what caused a `mouseover` event. The solution to your actual problem is probably to either avoid the layout changes, or improve the logic of your code. More specific approaches would require a more-specific question. – Alexander O'Mara Feb 28 '16 at 21:03
  • @AlexanderO'Mara if you turn this into an answer, I will accept it, that's what I was looking for. For short: Q:"is it possible...", A: "No, (not in a standard way)" – Gyro Gearloose Feb 28 '16 at 21:08
  • Not sure why you aren't getting the ID when the user clicks on it. That's the normal pattern. I want this--click--this is what was clicked on. What happens when someone with a palsy visits your site? I'm half serious about that. Selecting by hover is not a common UI pattern, and you should really be using common UI patterns in your website. –  Feb 29 '16 at 18:18
  • @Will the click itself works well, but the subsequent ajax call somehow is not processed as an atomic transaction: first the old image is deleted, second the browser does a layout-refresh with the image missing, third there is a chance that mouseover is checked for the current mouse position against the temporal incomplete layout, and forth, the new image is displayed with a new layout.. Thank you for your question, perhaps this may lead me to some understanding what could have been unclear. – Gyro Gearloose Feb 29 '16 at 23:29
  • *first the old image is deleted* or maybe you replace it with a loading image, thus keeping the container intact, thus you can find it again when the ajax call returns, thus that's the way I'd do it. Anyhow, good luck. –  Mar 01 '16 at 14:09
  • @Will, do I read your comment right as "put your image into a *fixed width* container? Good point! – Gyro Gearloose Mar 01 '16 at 21:36
  • Possibly. Wouldn't hurt if you're desperate. –  Mar 01 '16 at 21:50
  • @Will not desperate ... I'm just trying to learn about new ground. I'm only, but seriously, disturbed about by the hostility I face here. (hostility == trigger happiness to downvote or close without any willingness to communicate in any way.) – Gyro Gearloose Mar 01 '16 at 22:00
  • Desperate wasn't the best word. But anyhow. There are like 10k new questions every day. It's not the burden of others to explain why your question has issues, it's really your responsibility to make sure your question is clear, concise, shows what you have done and why it isn't working. Pick any upvoted javascript question and put it side by side with your original. It should be instantly clear what the differences are. This tag is hard to start out in, as the sheer number of awful js questions overwhelms those volunteering their help. –  Mar 01 '16 at 22:20
  • 1
    If I had one piece of advice to give someone in your position, it would be to create as small a prototype that exhibits your issue as possible and include that in your question. That shows effort and makes it abundantly clear what's going on. The ajax would be an issue, yes, but you can use a timeout to simulate an asynchronous call. Hell, I whip up a prototype every time I want to ask a question. 3/4 of the time I find my answer just by doing it. Try it next time. –  Mar 01 '16 at 22:22
  • @Will this is getting out of the "comment" scope. Would you be willing to switch to chat? – Gyro Gearloose Mar 01 '16 at 22:24
  • Nah, I've said all I think needs to be said. You'll be fine if you keep my advice in mind. Good luck. –  Mar 02 '16 at 13:18

1 Answers1

2

The short answer is you can't really tell what caused a mouseover event, other than the cursor moved over a new DOM element (or some other things, like a tap on a touch device). A layout change is just something that happens independent of this event.

One possible solution to your actual problem is to avoid the problem altogether by preventing the layout jump. For example, you could use a placeholder element to fill the space while the next image loads.

Alternately, your logic could almost-certainly be made more-robust so as not to depend on something as fragile as this.

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
  • 5
    @gyro Rather than implying that these 5 people are idiots who haven't even a clue, you would get further by taking their hint that your question *might* be unclear and editing to clarify. Of course it is not unclear to you what you want, but we are not in your head and don't know everything that you know. I'm happy that you were able to get your question answered this time, but you will not fare well in the future if you are unwilling to put forth the effort required when asking questions. – Cody Gray - on strike Feb 29 '16 at 06:14
  • @CodyGray perhaps you miss some parts of the story: I did edit the question. I did flag for moderator attention and specifically asked what was unclear. Non of these people did even care to leave a comment. (Not even a "still unclear"). – Gyro Gearloose Feb 29 '16 at 23:23
  • Those people had no idea that you'd done any of those things. All they knew was, the *first time* they looked at your question, it was unclear. What comment do you want someone to leave when they see a question that is unclear? *"This is unclear"*? That comment gets left implicitly when 4 other people agree with them voting to close the question as unclear. Editing the question is a good thing to do in response, but it doesn't recall those original 5 people. Nor should it. It gets a *new* group of people to look at your question, and clearly they *still* thought it was unclear. – Cody Gray - on strike Mar 01 '16 at 11:38
  • Flagging for moderator attention does absolutely no good. A moderator cannot tell you in response to a flag what is unclear about your question. You should have gotten the "still unclear" hint when your edit did not cause the question to be re-opened. Instead, you continued to insist that you were right and everyone else was wrong. I'm just saying, good luck with that. @gyro – Cody Gray - on strike Mar 01 '16 at 11:38
  • @CodyGray "Flagging for moderator attention does absolutely no good." I regret having to say you're absolutely right about that. – Gyro Gearloose Mar 01 '16 at 21:39
  • @CodyGray "Those people had no idea that you'd done any of those things." If so, what way it is supposed to work? I am required to improve the question, but no-one cares?!?! Please do give an answer! – Gyro Gearloose Mar 01 '16 at 21:41