Forum Discussion
[JS] SET OBJECT POSITION
Hello,
is it possible to set an object position (x,y) using javascript when, for example, user clicks the button?
35 Replies
- MarcinPaweczak-Community Member
A solution using GSAP or TweenLite to reposition elements may be useful. However, it has the feature that changing the page size in the browser (e.g. the F11 key) resets the element's position. Is there any workaround for this problem?
- MathNotermans-9Community Member
Yes. Use a browser event to trigger functions that reset/recalculate the position. Using 'vw' and 'vh' or % instead of pixels also helps.
- MarcinPaweczak-Community Member
Thank you very much for the tip. Onresize event works, % probably not.
- MathNotermans-9Community Member
These might help...
https://community.articulate.com/discussions/articulate-storyline/selecting-groups-of-elements-with-javascript-by-class
https://community.articulate.com/discussions/building-better-courses/animate-storyline-groups-with-gsap
https://community.articulate.com/discussions/building-better-courses/how-to-use-gsap-for-animation-in-storyline
https://360.articulate.com/review/content/bf35e314-c2a5-42c8-948d-4ab576847e5d/review- RobShaw-576b151Community Member
Math,
I've been playing with the code and while I can get it to work with one group, I can't figure out how to make it work with multiple groups.
The issue I've got is that I've got buttons with states, when grouped, the whole group changes state on hover and click.
I've tried separating them out into 12 groups, and I'm trying to use the code below to move the groups, but it doesn't work.
gsap.to( allGroups[0].parentElement, { duration:0.5, x: -808});
gsap.to( allGroups[1].parentElement, { duration:0.5, x: -692});
gsap.to( allGroups[2].parentElement, { duration:0.5, x: -576});
gsap.to( allGroups[3].parentElement, { duration:0.5, x: -468});
gsap.to( allGroups[4].parentElement, { duration:0.5, x: -459});
gsap.to( allGroups[5].parentElement, { duration:0.5, x: -344});
gsap.to( allGroups[6].parentElement, { duration:0.5, x: -344});
gsap.to( allGroups[7].parentElement, { duration:0.5, x: -229});
gsap.to( allGroups[8].parentElement, { duration:0.5, x: -113});
gsap.to( allGroups[9].parentElement, { duration:0.5, x: -17});
gsap.to( allGroups[10].parentElement, { duration:0.5, x: 96});
gsap.to( allGroups[11].parentElement, { duration:0.5, x: 212});
gsap.to( allGroups[12].parentElement, { duration:0.5, x: 328});
I should point out I've got the screen divided into 10 sections, with shapes reacting to hovers to move my group horizontally back and forth across the screen.
Using just one group [0] with all the objects inside it works fine but I get the hover issue.
Can you help?
R.- MathNotermans-9Community Member
HI Rob,
Not sure what you do different, but using this old one and adding buttons into the groups...all works nicely.
- RobShaw-576b151Community Member
Thank you Math, I'll take a look.
- MathNotermans-9Community Member
Version 3.11.3. Publish a Storyline... in the browser console type... gsap.version
And it will return what version is included.- RobShaw-576b151Community Member
Hi Math,
I came across your example for moving an object using x/y values.
How would I go about moving multiple objects. I have tried grouping layers and using the array ref for the grouped layer, but it will only move the first layer in the group and not all the others.
Would a while loop be able to go from layer 3 to 50, for example and then move all the objects on those layers?
Example code would be great if you're willing to look at this, I know this is an old thread...
R- MathNotermans-9Community Member
I mock up some better explanation tomorrow, but yes its possible.
Using the same acc-name for all elements and loop them and they will all animate.
Do check my samples im quite sure there is on showing this.
If not i mock up something tomorrow.
- MartinGregsonCommunity Member
Math, Yet again, YOU ARE THE MAN
Thank you!
- MathNotermans-9Community Member
Not sure for what but thanks anyway ;-)
- MartinGregsonCommunity Member
The reason is I was not aware of the inclusion of the GSAP in articulate!
Do you happen to know what version is included 2 or hopefully 3?
- ElizabethParentCommunity Member
Hi Math,
Thanks a lot. I'm very sorry, but I discussed this problem with my colleague and we found another solution, but I keep in mind this very useful solution for another project.
- MathNotermans-9Community Member
- MathNotermans-9Community Member
Yes should be possible. If you share a sample with your specific issue...i can easily show how.
- ElizabethParentCommunity Member
Hi,
I was wondering if i could move an element on a layer relative to another element on the base layer?
Thanks
- MathNotermans-9Community Member
Your surname aint coincidence for this kind of interactivity ;-) Its really parent-child behaviour of elements ;-)
Well made it happen. In fact i wanted rotation of the planets in our galaxy on SlideLayers as example, but especially rotation was a bit more tricky. I probably make it happen some time but not today ;-)
https://360.articulate.com/review/content/41f9e131-b5ba-4bef-8656-d57cd1d6f526/review
Here you see a sample of whats needed to get SlideLayers to act upon changes of objects on the baseLayer. In fact you only need to calculate the distance between the parent-child, so you could do it with less Javascript.
But for now showing a child on a SlideLayer moving along with a random movement of a parent on the baseLayer.
The rotation idea i will finish some other time.. here is the working Storyline too.- ElizabethParentCommunity Member
Hi Math,
If I move the item in the base layer (in a Drag and Drop freeform) can I get the proprety X and Y of my base item to set the X and Y for my item in the slideLayer?
- MathNotermans-9Community Member
Yes. With javascript for sure. Will make a sample tomorrow
- HannesKleefe600Community Member
If I try to rebuild Przemyslaws Sample it works, when publishing to Web, but not when publishing to LMS / LRS as SCORM 1.2. Can somebody explain to me why? And if there is a way to adjust it so it works?
- MathNotermans-9Community Member
Both work when i test them. So should work on the LMS too. Guess it depends on if the Scorm is shown in an iframe and thus maybe the selection used doesnot work.
Also although the selection that is used works fine.. its quite dependant on the amount of elements you have on your timeline. If you have less or more...the selector needs to be different.let pilka = document.getElementsByClassName("slideobject-maskable")[13];
The number 13 relates to layer 14 in the timeline... its array based, so 0-13 from the bottom....
So if you change the amount of elements... well it wont work. Or change the order...then something else will move.document.querySelector("#slide-window > div > div > div.slide-transition-container > div > div.slide-layer.base-layer.shown > div:nth-child(14) > div");
This is the selector as copied from the running HTML...
I use the accessibility names as selector...for a imported image thats always the original name.
So for the ball thats like this...let pilka = document.querySelector("[data-acc-text='path859.png']");
how many other elements you add, what layer its on...this works always.
- MathNotermans-9Community Member
GSAP's TweenLite is built into Storyline and thus can be used to move anything anywhere...
Here is a sample i recently made to demonstrate the use of TweenLite in Storyline. Basically all animation in Storyline is done the same way, Articulate uses it under the hood to create their timeline and motion path animations.
https://360.articulate.com/review/content/c9c0450c-0814-4492-bc02-5192e40017b9/review
The sample as is i attached too..