Jump to content
Search Community

Draggable - changing CSS properties onClick

d11202 test
Moderator Tag

Go to solution Solved by PointC,

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hi

 

I'm learning how to use Draggable.

I have some divs styled as coloured boxes with some basic text in.

<div id="answer1" class="box-answer"><span class="text">a ratio</span></div>

I use the following Javascript to facilitate the div being Draggable and then to change the colour of the box when it receives an onClick. I'm not sure why, but it doesn't seem to work

Draggable.create(".box-answer", {
	type:"x,y",
	bounds: "#container",
	throwProps:true,
	edgeResistance:0.35,
	onClick:function() {
		console.log("clicked", this.target.id );
		$( "#"+this.target.id ).css("background-color","#333333");
	},
	onDragEnd:function() {
		console.log("drag ended");
	}
});

If anyone can show me how I change the CSS properties of a Draggable element after receiving a mouseEvent I'd be very grateful.

 

Many thanks in advance

 

 

 

 

 

 

Link to comment
Share on other sites

  • Solution

Hi there d11202 :),

 

You didn't have a CodePen so I made a really quick one using your HTML above.

 

See the Pen qOpeEV by PointC (@PointC) on CodePen

 

I set the color change for a dragStart, but you can also set it for a click. 

 

I hope this helps.

 

PS For future reference - a CodePen is usually the fastest way for others to help with your questions. Check out this post on how to make one:

http://greensock.com/forums/topic/9002-read-this-first-how-to-create-a-codepen-demo/

  • Like 3
Link to comment
Share on other sites

Hi PointC

 

...just one last thing. Are you or anyone else able to say why the jQuery reference in my question would not change the CSS value of the Draggable element?

onClick:function() {
	console.log("clicked", this.target.id );
        $( "#"+this.target.id ).css("background-color","#333333");
}
Link to comment
Share on other sites

Perhaps it's because you're not accessing the ID properly - that's an attribute, so you'd need to use getAttribute() like:

$( "#"+this.target.getAttribute("id") ).css("background-color","#333333");

But actually, this would probably be cleaner:

$(this.target).css("background-color","#333333");

Or even: 

this.target.style.backgroundColor = "#333333";
  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...