Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find specific class out of multiple in jQuery

I am creating a js file that creates a ripple touch effect when you click an object. I want to assign a class to the element in the html code that is simply a color code like #f6ac32, and then create a function in Javascript/jQuery that can pick out that color code and create a variable out of it that I can later use (to change the color of the ripple effect). Is this possible?

Here is what I've made (check out the comment in $('.ripple').mousedown(function) ) :

$(document).ready(function() {

var rplObj,
	x,
	y,
	ink,
	color,
	rplDelTimer;



//fade out ripple when unclicked
$('.ripple').mouseup(function() {
	$('.ink').css({'opacity':'0'});
	delRipple();
})

//Delete ripple element one second after last click
function delRipple() {
	rplDelTimer = setTimeout(function() {
		$('.ink').remove();
	}, 1000)
}

$('body').mousemove(function(e){
	//update mouse coordinates when it is moved
	x = e.pageX;
	y = e.pageY;
})



$('.ripple').mousedown(function(){
	rplObj = $(this);
	color = "#FFF"; //I want this to dynamically change depending on the class written in html
	rippleClosed();
})



function rippleClosed() {
	rplObj.prepend('<span class="ink"></span>'); //create ripple		
	ink = rplObj.find('.ink'); //create variable for ink span
	ink.css('background',color); //set ripple color to color variable

	//set diameter of ripple
	if(!ink.height() && !ink.width()) {
		//set diameter to parents width/height (whichever is larger)
		d = Math.max(rplObj.outerWidth(), rplObj.outerHeight());
		ink.css({height: d, width: d});
	}

	//set coordinates of ripple using position of mouse defined earlier
	x = x - rplObj.offset().left - ink.width()/2;
	y = y - rplObj.offset().top - ink.height()/2;
	
	//set the position and animate the expansion
	ink.css({top: y+'px', left: x+'px'}).css({'transform':'scale(1.8)'});
	
	//reset ripple deletion timer
	clearTimeout(rplDelTimer);
}
  
})
div {
  background: #199ee3;
  width: 300px;
  height: 100px;
}

.ripple {
	position: relative;
	overflow: hidden;
}

.ink {
	position: absolute;
	border-radius: 100%;
	opacity: .4;
	transform: scale(0);
	-moz-transition: all  1s ease;
	-o-transition: all 1s ease;
	-webkit-transition: all  1s ease;
	transition: all  1s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="ripple"></div>
<p>Click the rectangle!</p>

Also, if you're curious, here's the site I'm using it for

like image 234
Alex Wohlbruck Avatar asked Jul 06 '15 04:07

Alex Wohlbruck


1 Answers

Don't use classes, use data attributes. You can store all kinds of arbitrary information directly on DOM nodes this way.

$('div').on('click', function () {
  var ripple = $(this).data('ripple');
  
  alert(ripple);
});
div {
  background: #199ee3;
  width: 300px;
  height: 100px;
}
<div data-ripple="#FFF"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
like image 98
Oka Avatar answered Sep 24 '22 20:09

Oka