Custom Data Attributes HTML JavaScript CSS Tutorial
Learn to use custom data attributes. Custom data attributes allow authors to create custom data to apply to their HTML elements when no other attributes make sense to use to store extra data. They help extend the information that html elements can communicate to a program or script. Custom data attributes can be accessed through both JavaScript and CSS.
example.html
<style>
body { background: #F3F3F3; }
div.rockets {
position: absolute;
background:url(rocket.jpg) no-repeat;
width: 125px;
height: 50px;
left: 0px;
transition: left 0s linear 0s;
}
div.rockets[data-altitude="100"] {
transform: rotate( 10deg );
}
</style>
<script>
var rockets, altitude, speed, distance;
function activateRockets(){
rockets = document.querySelectorAll('div.rockets');
for(var i=0; i < rockets.length; i++){
altitude = rockets[i].dataset.altitude;
speed = rockets[i].dataset.speed;
distance = rockets[i].dataset.rocketDistance;
rockets[i].style.top = altitude+"px";
rockets[i].style.transitionDuration = speed+"s";
rockets[i].style.left = distance+"px";
}
}
window.addEventListener("load", activateRockets);
</script>
<div class="rockets" data-altitude="100" data-speed="3.5" data-rocket-distance="700"></div>
<div class="rockets" data-altitude="200" data-speed="5.0" data-rocket-distance="700"></div>