Skip to content

Instantly share code, notes, and snippets.

@porfidev
Created August 26, 2016 23:38
Show Gist options
  • Save porfidev/d1f6d18feec7eed48dedbdce66157db6 to your computer and use it in GitHub Desktop.
Save porfidev/d1f6d18feec7eed48dedbdce66157db6 to your computer and use it in GitHub Desktop.
Cambiar la imagen de fondo con jQuery y CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.clase-miku {
background-image: url('anime-wallpaper-3.jpg');
}
.clase-hatsune {
background-image: url('anime-wallpaper-2.jpg');
}
.clase-sonico {
background-image: url('anime-wallpaper-1.jpg');
}
</style>
</head>
<body>
<button data-fondo="clase-hatsune"> Hatsune </button>
<button data-fondo="clase-miku"> Miku </button>
<button data-fondo="clase-sonico"> Sonico </button>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script>
$(function(){
var $body = $('body');
$body.find('button').on('click', function(){
var boton = $(this);
var claseAAplicar = boton.data('fondo');
aplicarClase(claseAAplicar);
});
function aplicarClase(unaclase){
$body.removeClass();
$body.addClass(unaclase);
}
// Inicializador
aplicarClase('clase-sonico');
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment