Hello I want to know how to make a slider with jquery, I'm quite new with this I will try my best to be specific, I want to do this:
I have an slider with 3 images "they are on the same space" and 3 bullets below the images, first I can't see the images because they have opacity: 0 but if I click the first bullet I will see the first image, bullet# 2 img# 2, bullet# 3 img# 3, I want to put an <a> tag for example avanzar that for advance to next image "it will be on the right side" and the left side will be to back to the last image that you saw, my question is how can I do that? I will need to keep the transition of them and how to join that to the bullets? because I need to use the tag <a> <-- the button "for me" or the bullets, it's difficult and I'm trying to do it, any help is welcome
Here is my html:
<!DOCTYPE html>
<html>
<head>
<title>Slider</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css">
<script src="js/jquery-2.1.4.min.js"></script>
<script src="js/slider.js"></script>
</head>
<body>
<div class="sliders">
back
next
<ul >
<li><img src="image_1.jpg"></li>
<li><img src="image_2.jpg"></li>
<li><img src="image_3.jpg"></li>
</ul>
<!-- <ul class="controles">
<li class="activa"> </li>
<li> </li>
<li> </li>
</ul> -->
</div>
<div class="sliders">
back
next
<ul>
<li><img src="image_1.jpg"></li>
<li><img src="image_2.jpg"></li>
<li><img src="image_3.jpg"></li>
</ul>
</div>
</body>
</html>
Here is the js:
$.fn.slider = function(config){
var nodos = this;
var delay = (typeof config.delay == "number")?parseInt(config.delay):4000;
for (var i = 0; i < nodos.length; i++) {
Slider(nodos[i]);
}
function Slider(nodo){
var galeria = $(nodo).find('ul');
if(!$(nodo).hasClass('slider'))
$(nodo).addClass('slider');
if(!galeria.hasClass('galeria'))
galeria.addClass("galeria");
//Encontrar cuantas imagenes hay en la galeria
var imagenes = $(galeria).find('li');
//Controles
var html_bullets="<ul class='controles'>";
for (var it = 0; it < imagenes.length; it++) {
html_bullets+="<li data-index='"+it+"'> </li>";
}
html_bullets+="</ul>";
$(nodo).append(html_bullets);
var bullets = $(nodo).find("ul.controles li");
bullets.click(function(){
var index= $(this).data("index");
bullets.removeClass('activa');
$(imagenes[index]).addClass("activa");
$(bullets[index]).addClass("activa");
});
}
};
$(document).ready(function() {
$(".sliders").slider({delay:5000});
});
Here is the css:
.slider{
width: 320px;
overflow: hidden;
}
.slider ul{
list-style: none;
padding: 0;
}
.slider ul.galeria{
height: 200px;
position: relative;
}
.slider ul.galeria li{
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: all 2s;
}
.slider ul.galeria li.activa{
opacity: 1;
}
.slider ul.galeria img{
max-height: 200px;
}
/*Controles*/
.slider ul.controles {
text-align: center;
}
.slider ul.controles li{
background-color: black ;
display: inline-block;
width: 20px;
height: 20px;
border-radius: 10px;
}
Here is what I wanted I already finished it, I'm glad if it helps someone
html:
<!DOCTYPE html>
<html>
<head>
<title>Practica2Jquery</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css">
<script src="jquery.js"></script>
<script src="slider.js"></script>
</head>
<body>
<div class="sliders">
Atras
<ul >
<li class="activa"><img src="image1.jpg"></li>
<li><img src="image2.jpg"></li>
<li><img src="image3.jpg"></li>
</ul>
Siguiente
</div>
<div class="sliders">
Atras
<ul>
<li class="activa"><img src="image4.jpg"></li>
<li><img src="image5.jpg"></li>
<li><img src="image6.jpg"></li>
</ul>
Siguiente
</div>
</body>
</html>
js:
$.fn.slider = function(config){
var nodos = this;
var delay = (typeof config.delay === "number")?parseInt(config.delay):3000;
for (var i = 0; i < nodos.length; i++) {
Slider(nodos[i]);
}
function Slider(nodo){
var galeria = $(nodo).find('ul');
var tag = "<a class='atras'></a>";
if(!$(nodo).hasClass('slider'))
$(nodo).addClass('slider');
if(!galeria.hasClass('galeria'))
galeria.addClass("galeria");
//Encontrar cuantas imagenes hay en la galeria
var imagenes = $(galeria).find('li');
//Controles
var html_bullets="<ul class='controles'>";
for (var it = 0; it < imagenes.length; it++) {
if(it==0)
html_bullets+="<li data-index='"+it+"' class='activa'> </li>";
else
html_bullets+="<li data-index='"+it+"'> </li>";
}
html_bullets+="</ul><a class='siguiente'></a>";
$(nodo).append(html_bullets);
$(nodo).prepend(tag);
var bullets = $(nodo).find("ul.controles li");
bullets.click(function(){
var index= $(this).data("index");
$(imagenes).add(bullets).removeClass('activa');
$(imagenes[index]).add(bullets[index]).addClass('activa');
});
}
$(".slider").on("click","a.atras",function(){
direcciones({div: this.parentElement});
});
$(".slider").on("click","a.siguiente",function(){
direcciones({div: this.parentElement,direccion:1});
});
function direcciones(lado){
var div = lado.div;
var imagen = $(div).find("ul.galeria li.activa");
var imagenes = $(div).find("ul.galeria li");
var bullet = $(div).find("ul.controles li.activa");
var bullets = $(div).find("ul.controles li");
var index = bullet.data("index");
var max = bullets.length-1;
$(bullets).add(imagenes).removeClass('activa');
if(lado.direccion){
// === ?
if(index == max){
$(imagenes[0]).add(bullets[0]).addClass('activa');
}else {
$(imagenes[index+1]).add(bullets[index+1]).addClass('activa');
}
}
else{
if(index == 0){
$(imagenes[max]).add(bullets[max]).addClass('activa');
}else {
$(imagenes[index-1]).add(bullets[index-1]).addClass('activa');
} } } };
$(document).ready(function() {
$(".sliders").slider({delay:3000});
});
css:
.slider{
width: 420px;
overflow: hidden;
}
.slider ul{
list-style: none;
padding: 0;
}
.slider ul.galeria{
height: 200px;
position: relative;
margin-left: 30px;
}
.slider ul.galeria li{
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: all 2s;
}
.slider ul.galeria li.activa{
opacity: 1;
}
.slider ul.galeria img{
max-height: 400px;
max-width: 300px;
margin-left: 5px;
}
/*Controles*/
.slider ul.controles {
position: relative;
left: 38%;
bottom: 45px;
}
.slider ul.controles li{
background-color: black ;
display: inline-block;
width: 20px;
height: 20px;
border-radius: 10px;
}
.slider ul.controles li.activa{
background-color: red ;
}
.slider a.atras{
position: relative;
left: 0;
top: 128px;
}
.slider a.siguiente{
position: relative;
left: 80%;
bottom: 120px;
}
Related
I think I got all the basic functionalities working fine for this little todo list project, but when I open it on my browser (Chrome) using VSCode, some functionalities seem to not loading properly. For example, in my case, when first opened on the browser, the cursor when hovering on "Add Task" is supposed to be "pointer" as you can see in CSS under #addTask ID, yet it still seems to be 'auto'.
I notice that in order for all functionalities to work properly, I must click on other tabs after opening the project on the browser.
Has anybody found themselves in a similar situation before? Or is it just me?
Thanks, guys.
P.S I'm using a MacBook Pro Retina Late 2012 High Sierra.
Does this ever happen to anybody?
var remove = function(){
this.parentNode.removeChild(this);
}
function addTask(){
document.getElementById('deleteTask').textContent = 'Delete Task';
var lis = document.querySelectorAll('li');
for(var i = 0 ; i < lis.length; i ++){
var li = lis[i];
li.style.cursor = 'pointer';
li.removeEventListener('click', remove);
}
//Get the element from input field
var li = document.createElement('li'); // create a <li> </li>
var userInput = document.getElementById('userInput').value; // Get the input from User
if(userInput.trim() != ''){
var userText = document.createTextNode(userInput);
li.appendChild(userText);
var ul = document.getElementById('unorder_list');
ul.appendChild(li);
} else {
alert('Field cannot be empty');
}
document.getElementById('userInput').value = '';
var lis = document.querySelectorAll('li');
for(var i = 0 ; i < lis.length ; i ++){
lis[i].style.cursor = 'auto';
}
}
function deleteAll(){
var ul = document.getElementById('unorder_list');
ul.innerHTML = '';
}
function deleteTask(){
document.getElementById('deleteTask').textContent = 'Click to delete';
//Click this button first, all the <li> is now clickable and when you click on one, it disappears!
var lis = document.querySelectorAll('li');
for(var i = 0 ; i < lis.length; i ++){
var li = lis[i];
li.style.cursor = 'pointer';
li.addEventListener('click', remove);
}
}
*, *:before, *:after{
margin: 0;
padding: 0;
box-sizing: border-box;
}
#title{
position: relative;
text-align: center;
top: 100px;
font-size: 90px;
}
input{
position: relative;
width: 30%;
text-align: center;
top: 100px;
left: 30%;
}
#addTask{
position: relative;
top: 100px;
left: 30%;
border-style: solid;
border-color: green;
border-radius: 10%;
cursor: pointer;
}
#deleteTask{
position: relative;
top: 100px;
left:30%;
border-style: solid;
border-color: red;
border-radius: 10%;
cursor: pointer;
}
#deleteAll{
position: relative;
top: 140px;
text-align: center;
border-style: solid;
cursor: pointer;
width: 40%;
margin: 0 auto;
}
ul{
position: relative;
top: 120px;
left: 25%;
width: 50%;
border-style: solid;
border-color: black;
}
ul li{
list-style-type: none;
padding: 5px;
}
li:nth-child(odd){
background: rgb(219, 169, 169);
}
ul li:hover{
transition: 1s;
background: greenyellow;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Andy">
</head>
<body>
<div id="container">
<div id="title">Your to-do list</div>
<input type='text' id="userInput" placeholder="Hmm, what should I do next...?"/>
<span id="deleteTask" onclick="deleteTask()">Delete Task</span>
<span id="addTask" onclick="addTask()">Add Task</span>
<ul id="unorder_list">
</ul>
<div id="deleteAll" onclick="deleteAll()">Delete All Task</div>
</div>
</body>
<script src="todo.js"></script>
</html>
I have an image/video carousel and I'm having trouble keeping all the content in the carousel the same height.
When you click on the thumbnails, the main image heights don't match up.
How do I keep everything the same height, yet responsive at the same time?
(I added some pictures to show what I mean)
Here's the codepen.
Edit: I want to keep the main images shown in the carousel the same height, not the thumbnails.
$(document).ready(function() {
// get all images loaded
var images = $(".chair-class");
// thumbnails containers
var thumbnailContainer = $("#thumbnails");
var iframe1 = $('#sketchfab-iframe-1')[0];
var iframe2 = $('#sketchfab-iframe-2')[0];
var player1 = $f(iframe1);
var player2 = $f(iframe2);
// generate thumbnail images
generateThumbnails(images, thumbnailContainer);
// listeners for controls arrows
$(".prev-next-button").on("click", function() {
player1.api('pause');
player2.api('pause');
// get the images
var currentImageIndex = images.index($(".chair-class[data-active=true]"));
var isPrevious = $(this).hasClass("previous");
var nextIndex;
if (isPrevious) {
if (currentImageIndex === 0) {
nextIndex = images.length - 1;
}
if (currentImageIndex > 0) {
nextIndex = currentImageIndex - 1;
}
} else {
if (currentImageIndex === images.length - 1) {
nextIndex = 0;
}
if (currentImageIndex < images.length - 1) {
nextIndex = currentImageIndex + 1;
}
}
// remove any active class from images
images.removeClass("active").attr('data-active', "false");
// get the next active image and add active class to that next current image
var $nextImage = $(images[nextIndex]);
var iframeId = $nextImage.data('iframe');
if (iframeId) {
$(images[nextIndex]).attr('data-active', "true");
$('.sketchfab-iframe').removeClass('active');
$('#' + iframeId).addClass('active');
} else {
$(images[nextIndex]).addClass("active").attr('data-active', "true");
$('.sketchfab-iframe').removeClass('active');
}
});
$(".thumb").on("click", function(event) {
event.preventDefault();
var images = $(".chair-class");
var indexSelected = $(this).data("img-index");
var currentShown = images.index($(".chair-class[data-active=true]"));
if (currentShown === indexSelected) return false;
player1.api('pause');
player2.api('pause');
images.removeClass("active").attr('data-active', "false");
var iframeId = $(this).data('iframe');
if (iframeId) {
$(images[indexSelected]).attr('data-active', "true");
$('.sketchfab-iframe').removeClass('active');
$('#' + iframeId).addClass('active');
} else {
images.removeClass("active");
$(images[indexSelected]).addClass('active').attr('data-active', "true");;
$('.sketchfab-iframe').removeClass('active');
}
});
function generateThumbnails(images, container) {
var ul = $("<ul>");
images.each(function(index, element) {
var currentThumb = $("<img>");
var li = $("<li>");
var src = $(this).attr("src");
currentThumb.attr("src", src);
currentThumb.attr("class", "thumb");
currentThumb.data("img-index", index);
var iframe = $(this).data('iframe');
if (iframe) {
currentThumb.data("iframe", iframe);
}
li.append(currentThumb);
ul.append(li);
});
container.append(ul);
}
});
* {
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 0;
font-size: 100%;
}
/* #green-room {
background: #333 !important;
} */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
#chair,
.chair-class {
position: absolute;
width: 100%;
height: auto;
max-width: 600px;
max-height: 400px;
margin: 0 auto;
display: block;
top: 0;
left: 0;
opacity: 0;
transition: opacity .5s;
}
.chair-class.active {
position: relative;
opacity: 1;
}
#prev {
position: absolute;
color: black;
left: 0;
top: 0;
bottom: 0;
}
#next {
position: absolute;
color: black;
right: 0;
top: 0;
bottom: 0;
}
#prev p,
#next p {
font-size: 3em;
}
#imgDetail {
position: relative;
width: 90%;
margin: 0 auto;
overflow: hidden;
}
#imgDetail a {
text-decoration: none;
display: flex;
padding: 3%;
justify-content: center;
align-items: center;
}
#imgDetail a:hover {
background-color: #333;
color: white;
opacity: 0.5;
}
#imgDetail ul {
margin: 0 auto;
display: block;
}
#thumbnails {
max-width: 1000px;
width: 100%;
display: inline-block;
text-align: center;
}
.thumb {
width: 21%;
height: auto;
margin-top: 15px;
cursor: pointer;
}
#imgDetail li {
display: inline;
}
#thumbnails ul {
margin: 0 auto;
display: block;
}
#thumbnails li {
display: inline;
padding-right: 2%;
}
#thumbnails li:last-child {
padding-right: 0;
}
.sketchfab-iframe {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 40vw;
}
.sketchfab-iframe {
opacity: 0;
margin: 0 auto;
transition: opacity .5s;
display: none;
}
.sketchfab-iframe.active {
opacity: 1;
position: relative;
display: block;
}
<script src="https://f.vimeocdn.com/js/froogaloop2.min.js"></script>
<script src="https://f.vimeocdn.com/js/froogaloop2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Images not Owned by Me -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Daniel Pollack</title>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<script src="https://unpkg.com/scrollreveal/dist/scrollreveal.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<script src="http://a.vimeocdn.com/js/froogaloop2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fitvids/1.2.0/jquery.fitvids.js">
</script>
</head>
<body id="green-room">
<div class="slideshow-container">
<div id="imgDetail">
<img data-iframe="sketchfab-iframe-1" src="https://i.vimeocdn.com/video/682901345_640.webp" class="chair-class" data-active="true" />
<img data-iframe="sketchfab-iframe-2" src="https://i.vimeocdn.com/video/682890362_640.webp" class="chair-class" data-active="false" />
<img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_3.jpg" class="chair-class" data-active="false" />
<div class="aspect-ratio">
<iframe id="sketchfab-iframe-1" class="sketchfab-iframe active" src="https://player.vimeo.com/video/255482396" width="100%" height="400" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<div class="aspect-ratio">
<iframe id="sketchfab-iframe-2" class="sketchfab-iframe" src="https://player.vimeo.com/video/255473875" width="100%" height="400" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<!--CONTROLS-->
<a href="javascript:void(0)" id="prev" class="prev-next-button previous">
<p>❬</p>
</a>
<a href="javascript:void(0)" id="next" class="prev-next-button next">
<p>❭</p>
</a>
</div>
<!--Thumbnails-->
<div id="thumbnails">
</div>
</html>
It's fixed by setting all the image height to 70vh.
Guess something is not working with StackOverflow Fiddle.
Check my codepen and let me know if that's what you are looking for.
$(document).ready(function() {
// get all images loaded
var images = $(".chair-class");
// thumbnails containers
var thumbnailContainer = $("#thumbnails");
var iframe1 = $('#sketchfab-iframe-1')[0];
var iframe2 = $('#sketchfab-iframe-2')[0];
var player1 = $f(iframe1);
var player2 = $f(iframe2);
// generate thumbnail images
generateThumbnails(images, thumbnailContainer);
// listeners for controls arrows
$(".prev-next-button").on("click", function() {
player1.api('pause');
player2.api('pause');
// get the images
var currentImageIndex = images.index($(".chair-class[data-active=true]"));
var isPrevious = $(this).hasClass("previous");
var nextIndex;
if (isPrevious) {
if (currentImageIndex === 0) {
nextIndex = images.length - 1;
}
if (currentImageIndex > 0) {
nextIndex = currentImageIndex - 1;
}
} else {
if (currentImageIndex === images.length - 1) {
nextIndex = 0;
}
if (currentImageIndex < images.length - 1) {
nextIndex = currentImageIndex + 1;
}
}
// remove any active class from images
images.removeClass("active").attr('data-active', "false");
// get the next active image and add active class to that next current image
var $nextImage = $(images[nextIndex]);
var iframeId = $nextImage.data('iframe');
if (iframeId) {
$(images[nextIndex]).attr('data-active', "true");
$('.sketchfab-iframe').removeClass('active');
$('#' + iframeId).addClass('active');
} else {
$(images[nextIndex]).addClass("active").attr('data-active', "true");
$('.sketchfab-iframe').removeClass('active');
}
});
$(".thumb").on("click", function(event) {
event.preventDefault();
var images = $(".chair-class");
var indexSelected = $(this).data("img-index");
var currentShown = images.index($(".chair-class[data-active=true]"));
if (currentShown === indexSelected) return false;
player1.api('pause');
player2.api('pause');
images.removeClass("active").attr('data-active', "false");
var iframeId = $(this).data('iframe');
if (iframeId) {
$(images[indexSelected]).attr('data-active', "true");
$('.sketchfab-iframe').removeClass('active');
$('#' + iframeId).addClass('active');
} else {
images.removeClass("active");
$(images[indexSelected]).addClass('active').attr('data-active', "true");;
$('.sketchfab-iframe').removeClass('active');
}
});
function generateThumbnails(images, container) {
var ul = $("<ul>");
images.each(function(index, element) {
var currentThumb = $("<img>");
var li = $("<li>");
var src = $(this).attr("src");
currentThumb.attr("src", src);
currentThumb.attr("class", "thumb");
currentThumb.data("img-index", index);
var iframe = $(this).data('iframe');
if (iframe) {
currentThumb.data("iframe", iframe);
}
li.append(currentThumb);
ul.append(li);
});
container.append(ul);
}
});
* {
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 0;
font-size: 100%;
}
/* #green-room {
background: #333 !important;
} */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
#chair,
.chair-class {
position: absolute;
width: auto;
height: 100%;
/* max-width: 600px;
max-height: 400px; */
margin: 0 auto;
display: block;
top: 0;
left: 0;
opacity: 0;
transition: opacity .5s;
}
.chair-class.active {
position: relative;
opacity: 1;
}
#prev {
position: absolute;
color: black;
left: 0;
top: 0;
bottom: 0;
}
#next {
position: absolute;
color: black;
right: 0;
top: 0;
bottom: 0;
}
#prev p,
#next p {
font-size: 3em;
}
#imgDetail {
position: relative;
width: 90%;
margin: 0 auto;
height: 70vh;
overflow: hidden;
}
#imgDetail img {
height: 70vh;
}
#imgDetail a {
text-decoration: none;
display: flex;
padding: 3%;
justify-content: center;
align-items: center;
}
#imgDetail a:hover {
background-color: #333;
color: white;
opacity: 0.5;
}
#imgDetail ul {
margin: 0 auto;
display: block;
}
#thumbnails {
max-width: 1000px;
display: inline-block;
text-align: center;
}
.thumb {
width: 21%;
height: auto;
margin-top: 15px;
cursor: pointer;
}
#imgDetail li {
display: inline;
}
#thumbnails ul {
margin: 0 auto;
display: block;
}
#thumbnails li {
display: inline;
padding-right: 2%;
}
#thumbnails li:last-child {
padding-right: 0;
}
.sketchfab-iframe {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
/* top:10vh; */
height: 70vh;
opacity: 0;
margin: 0 auto;
transition: opacity .5s;
display: none;
}
.sketchfab-iframe.active {
opacity: 1;
position: relative;
display: block;
}
<script src="https://f.vimeocdn.com/js/froogaloop2.min.js"></script>
<script src="https://f.vimeocdn.com/js/froogaloop2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Images not Owned by Me -->
<body>
<div class="slideshow-container">
<div id="imgDetail">
<img data-iframe="sketchfab-iframe-1" src="https://i.vimeocdn.com/video/682901345_640.webp" class="chair-class" data-active="true" />
<img data-iframe="sketchfab-iframe-2" src="https://i.vimeocdn.com/video/682890362_640.webp" class="chair-class" data-active="false" />
<img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_3.jpg" class="chair-class" data-active="false" />
<div class="aspect-ratio">
<iframe id="sketchfab-iframe-1" class="sketchfab-iframe active" src="https://player.vimeo.com/video/255482396" width="100%" height="400" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<div class="aspect-ratio">
<iframe id="sketchfab-iframe-2" class="sketchfab-iframe" src="https://player.vimeo.com/video/255473875" width="100%" height="400" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<!--CONTROLS-->
<a href="javascript:void(0)" id="prev" class="prev-next-button previous">
<p>❬</p>
</a>
<a href="javascript:void(0)" id="next" class="prev-next-button next">
<p>❭</p>
</a>
</div>
<!--Thumbnails-->
<div id="thumbnails">
</div>
</div>
</body>
$(document).ready(function () {
// get all images loaded
var images = $(".chair-class");
// thumbnails containers
var thumbnailContainer = $("#thumbnails");
var iframe1 = $('#sketchfab-iframe-1')[0];
var iframe2 = $('#sketchfab-iframe-2')[0];
var player1 = $f(iframe1);
var player2 = $f(iframe2);
// generate thumbnail images
generateThumbnails(images, thumbnailContainer);
// listeners for controls arrows
$(".prev-next-button").on("click", function () {
player1.api('pause');
player2.api('pause');
// get the images
var currentImageIndex = images.index($(".chair-class[data-active=true]"));
var isPrevious = $(this).hasClass("previous");
var nextIndex;
if (isPrevious) {
if (currentImageIndex === 0) {
nextIndex = images.length - 1;
}
if (currentImageIndex > 0) {
nextIndex = currentImageIndex - 1;
}
} else {
if (currentImageIndex === images.length - 1) {
nextIndex = 0;
}
if (currentImageIndex < images.length - 1) {
nextIndex = currentImageIndex + 1;
}
}
// remove any active class from images
images.removeClass("active").attr('data-active', "false");
// get the next active image and add active class to that next current image
var $nextImage = $(images[nextIndex]);
var iframeId = $nextImage.data('iframe');
if (iframeId) {
$(images[nextIndex]).attr('data-active', "true");
$('.sketchfab-iframe').removeClass('active');
$('#' + iframeId).addClass('active');
} else {
$(images[nextIndex]).addClass("active").attr('data-active', "true");
$('.sketchfab-iframe').removeClass('active');
}
});
$(".thumb").on("click", function (event) {
event.preventDefault();
var images = $(".chair-class");
var indexSelected = $(this).data("img-index");
var currentShown = images.index($(".chair-class[data-active=true]"));
if (currentShown === indexSelected) return false;
player1.api('pause');
player2.api('pause');
images.removeClass("active").attr('data-active', "false");
var iframeId = $(this).data('iframe');
if (iframeId) {
$(images[indexSelected]).attr('data-active', "true");
$('.sketchfab-iframe').removeClass('active');
$('#' + iframeId).addClass('active');
} else {
images.removeClass("active");
$(images[indexSelected]).addClass('active').attr('data-active', "true");;
$('.sketchfab-iframe').removeClass('active');
}
});
function generateThumbnails(images, container) {
var ul = $("<ul>");
images.each(function (index, element) {
var currentThumb = $("<img>");
var li = $("<li>");
var src = $(this).attr("src");
currentThumb.attr("src", src);
currentThumb.attr("class", "thumb");
currentThumb.data("img-index", index);
var iframe = $(this).data('iframe');
if (iframe) {
currentThumb.data("iframe", iframe);
}
li.append(currentThumb);
ul.append(li);
});
container.append(ul);
}
});
* {
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 0;
font-size: 100%;
}
/* #green-room {
background: #333 !important;
} */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
#chair, .chair-class {
position: absolute;
width: 80%;
height: auto;
max-width: 600px;
max-height: 400px;
margin: 0 auto;
display: block;
top: 0;
left: 0;
right: 0;
opacity: 0;
transition: opacity .5s;
}
.chair-class.active {
position: relative;
opacity: 1;
}
#prev {
position: absolute;
color: black;
left: 0;
top: 0;
bottom: 0;
}
#next {
position: absolute;
color: black;
right: 0;
top: 0;
bottom: 0;
}
#prev p, #next p {
font-size: 3em;
}
#imgDetail {
position: relative;
width: 90%;
margin: 0 auto;
overflow: hidden;
}
#imgDetail a {
text-decoration: none;
display: flex;
padding: 3%;
justify-content: center;
align-items: center;
}
#imgDetail a:hover {
background-color: #333;
color: white;
opacity: 0.5;
}
#imgDetail ul {
margin: 0 auto;
display: block;
}
#thumbnails {
max-width: 1000px;
width: 100%;
display: inline-block;
text-align: center;
}
.thumb {
width: 21%;
height: 120px;
margin-top: 15px;
cursor: pointer;
}
#imgDetail li {
display: inline;
}
#thumbnails ul {
margin: 0 auto;
display: block;
}
#thumbnails li {
display: inline;
padding-right: 2%;
}
#thumbnails li:last-child {
padding-right: 0;
}
.sketchfab-iframe {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 40vw;
}
.sketchfab-iframe {
opacity: 0;
margin: 0 auto;
transition: opacity .5s;
display: none;
}
.sketchfab-iframe.active {
opacity: 1;
position: relative;
display: block;
}
<script src="https://f.vimeocdn.com/js/froogaloop2.min.js"></script>
<script src="https://f.vimeocdn.com/js/froogaloop2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Images not Owned by Me -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Daniel Pollack</title>
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
<script src="https://unpkg.com/scrollreveal/dist/scrollreveal.min.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script>
<script src="http://a.vimeocdn.com/js/froogaloop2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fitvids/1.2.0/jquery.fitvids.js">
</script>
</head>
<body id="green-room">
<div class="slideshow-container">
<div id="imgDetail">
<img data-iframe="sketchfab-iframe-1" src="https://i.vimeocdn.com/video/682901345_640.webp" class="chair-class" data-active="true" />
<img data-iframe="sketchfab-iframe-2" src="https://i.vimeocdn.com/video/682890362_640.webp" class="chair-class" data-active="false" />
<img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_3.jpg" class="chair-class" data-active="false" />
<div class="aspect-ratio">
<iframe id="sketchfab-iframe-1" class="sketchfab-iframe active" src="https://player.vimeo.com/video/255482396" width="100%" height="400" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<div class="aspect-ratio">
<iframe id="sketchfab-iframe-2" class="sketchfab-iframe" src="https://player.vimeo.com/video/255473875" width="100%" height="400" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<!--CONTROLS-->
<a href="javascript:void(0)" id="prev" class="prev-next-button previous">
<p>❬</p>
</a>
<a href="javascript:void(0)" id="next" class="prev-next-button next">
<p>❭</p>
</a>
</div>
<!--Thumbnails-->
<div id="thumbnails">
</div>
</html>
I basically want the centre image in the slider to be twice as big as the others. I need to be able to pass the class active to the centre image and remove it from the last image. So that the centre image is always twice as big. Is it possible to do this with my code?
$(document).ready(function(){
// function active_class(){
// $(".active").next().addClass('active');
// $(".img_cont").index(1).addClass('jiji');
// }
// active_class();
var slide_count = $(".carousel li").length;
var slide_width = $(".carousel li").width();
var slide_height = $(".carousel li").height();
var total_width = slide_width * slide_count;
$(".cont").css({ height: slide_height, width: slide_width, paddingLeft: slide_width , paddingRight: slide_width });
$(".carousel").css({ width: total_width, marginLeft: - slide_width});
$(".carousel li:last-child").prependTo(".carousel");
$("#next").css("right", slide_width);
$("#prev").css("left", slide_width);
function next_slide(){
$(".carousel").animate({
left: + slide_width
}, 400, function(){
$(".carousel li:last-child").prependTo(".carousel");
$('.carousel').css('left', 0);
}
);
}
function prev_slide(){
$(".carousel").animate({
left: - slide_width
}, 400, function(){
$(".carousel li:first-child").appendTo(".carousel");
$(".carousel").css("left", 0);
}
);
}
$("#next").click(function(){
next_slide();
});
$("#prev").click(function(){
prev_slide();
});
var interval_time = 4000; // 1s
var mouse_hover_flag = false;
$(".cont").hover(function(){
mouse_hover_flag = true;
}, function () {
mouse_hover_flag = false;
});
setInterval(function() {
if (!mouse_hover_flag) {//if not true
next_slide();
}
}, interval_time);
});
*{
padding: 0;
margin: 0;
}
body{
margin: 0;
padding: 0;
}
.cont{
position: relative;
text-align: center;
font-size: 0;/*removes white space*/
margin: 60px auto 0 auto;
padding: 0;
overflow: hidden;
}
.carousel{
position: relative;
margin: 0;
padding: 0;
list-style-type: none;
height: 100%;
max-height: 600px;
}
.carousel li{
float: left;
width: 750px;
height: 350px;
}
.carousel li img{
width: 100%;
height: auto;
}
#next{
position: absolute;
top: 45%;
right: 0;
width: 40px;
height: 40px;
background-color: blue;
font-size: 0;
z-index: 1;
}
#prev{
position: absolute;
top: 45%;
left: 0;
width: 40px;
height: 40px;
background-color: blue;
z-index: 1;
}
.img_cont{
transform: scale(0.5);
}
.active{
transform: scale(1);
}
<div class="cont">
<div id="next">
</div>
<div id="prev">
</div>
<ul class="carousel">
<li class="img_cont active">
<img src="http://lorempixel.com/output/abstract-q-c-1500-700-2.jpg" alt="" />
</li>
<li class="img_cont">
<img src="http://lorempixel.com/output/abstract-q-c-1500-700-6.jpg" alt="" />
</li>
<li class="img_cont">
<img src="http://lorempixel.com/output/abstract-q-c-1500-700-1.jpg" alt="" />
</li>
<li class="img_cont">
<img src="http://lorempixel.com/output/abstract-q-c-1500-700-3.jpg" alt="" />
</li>
</ul>
</div>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
https://codepen.io/Reece_Dev/pen/OgQLjE
It's easy to remove active class from all li elements, select the first and add the class again.
For example:
function next_slide(){
$(".carousel").animate({
left: + slide_width
}, 400, function(){
$(".carousel li:last-child").prependTo(".carousel");
$('.carousel').css('left', 0);
// Add the following line to prev_slide too:
$('.carousel li').removeClass('active').eq(0).addClass('active');
}
);
}
Hi i'm creating a jquery slider and i'm having problems to restar the slider when the time(in the setInterval) is over. When the 3 images slide and the last one is showing the animation stays there, but i want to restart to the first image and start the loop again. Could someone help me?
Thanks!
HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Slider Jquery Test</title>
<link href="slider.css" rel="stylesheet" type="text/css">
<script src="js/jquery-2.1.4.min.js"></script>
<script src="js/slidertest.js"></script>
</head>
<body>
<div id="container">
<ul id="buttons" class="buttons">
<li><img id="left_b" class="left_b" src="imagens/flechae.png" alt="Anterior" onMouseOver="this.src='imagens/flechaehover.png'" onMouseOut="this.src='imagens/flechae.png'"></li>
<li><img id="right_b" class="right_b" src="imagens/flechad.png" alt="Proximo" onMouseOver="this.src='imagens/flechadhover.png'" onMouseOut="this.src='imagens/flechad.png'"></li>
</ul>
<ul id="gallery" class="gallery">
<li><img id="1" class="images" src="imagens/imagem1.jpg" alt="Imagem 1"></li>
<li><img id="2" class="images" src="imagens/imagem2.jpg" alt="Imagem 2"></li>
<li><img id="3" class="images" src="imagens/imagem3.jpg" alt="Imagem 3"></li>
</ul>
</div>
</body>
</html>
CSS
#charset "utf-8";
body{
width: 100%;
height: auto;
}
#container {
width: 100%;
height: auto;
margin: 0;
padding: 0;
/*overflow: hidden;*/
}
.buttons {
width: 100%;
height: auto;
list-style: none;
margin: 0;
padding: 0;
position: absolute;
z-index: 1;
display: none;
}
.buttons li {
width: 100%;
height: auto;
position: relative;
margin: 0;
padding: 0;
}
.left_b {
width: 20px;
height: 80px;
position: relative;
float: left;
margin-left: 30px;
margin-top: 250px;
}
.right_b {
width: 20px;
height: 80px;
position: relative;
float: right;
margin-right: 30px;
margin-top: 250px;
}
.gallery {
width: 100%;
height: auto;
list-style: none;
margin: 0;
padding: 0;
position: relative;
overflow: hidden;
}
.gallery li {
width: auto;
height: auto;
position: relative;
float: left;
}
.images {
width: 1200px;
height: 720px;
position: relative;
float: left;
}
JavaScript
$(function(){
slide();
// Slider
function slide()
{
// Variables
var interval = 0;
var time = 3000;
var seconds = 1000;
var current_image = 1;
var num_images = 0;
var total_width = 0;
var slide_left = "+=1200px";
var slide_right = "-=1200px";
var right_b = $(".right_b");
var left_b = $(".left_b");
var left = "margin-left";
var width = "width";
var gallery = $(".gallery");
var galleryLi = $(".gallery li");
var images = $(".images");
var buttons_class = $(".buttons");
var container = $("#container");
// Calling functions
imagesSize();
hideButtons();
animation();
// Functions
function animation()
{
if(current_image < num_images)
{
loop();
clickRight();
clickLeft();
}
}
function hideButtons()
{
container.hover(function(){
buttons_class.fadeIn();
}, function(){
buttons_class.fadeOut();
});
}
function imagesSize()
{
galleryLi.each(function(){
num_images++;
total_width += 1200;
});
gallery.css(width, total_width + "px");
}
function loop()
{
if(current_image >= 1){
interval = setInterval(moveLeft, time);
}
else if(current_image === num_images){
clearLoop();
moveLeft();
}
}
function clearLoop()
{
clearInterval(interval);
}
function moveLeft()
{
if(current_image < num_images){
gallery.animate({left: slide_right}, seconds);
current_image++;
}
}
function clickRight()
{
right_b.click(function(){
moveLeft();
clearLoop();
loop();
});
}
function moveRight()
{
if(current_image > 1){
gallery.animate({"margin-left": slide_left}, seconds);
current_image--;
}
}
function clickLeft()
{
left_b.click(function(){
moveRight();
clearLoop();
loop();
});
}
} // end of function slide
}); // End of main function
function loop()
{
if(current_image >= 1){
interval = setInterval(moveLeft, time);
}
else if(current_image === num_images){
clearLoop(); // Clear the previous interval
moveLeft();
// Add these two lines after setTimeout to finish the animation.
setTimeout(function(){
current_image = 1;
loop();
}, seconds);
}
}
I am extremely new to JQuery, I just started looking into it today. I have searched all around for what might be causing this bit of code to not work. When you scroll down, I want the h1 to move to the side and a menu button to appear. That works, but when I scroll back up again, it takes an extremely long time to reverse itself. I have tried to fine anything that might be causing it like a delay or something, but as far as I can see, there isn't any problems.
Link to website: http://www.dragonmath.net/rockets
Here is my code:
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles/main.css" />
<title>Rockets</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<header>
<img id="menu" src="images/menu1.png" />
<div id="headerdiv">
<h1>Rockets</h1>
<img id="logo" src="images/logo1.png" />
</div>
</header>
<nav>
<ul>
<li>
<a>Space Race</a>
</li>
<li>
<a>SpaceX</a>
</li>
</ul>
</nav>
<script>
$( document ).ready(function() {
var $menu = $('img#menu');
var $headerdiv = $("div#headerdiv")
var $nav = $('nav');
$(window).on('scroll', function () {
if ($(window).scrollTop() > 40) {
$headerdiv.addClass("testheaderdiv");
$menu.delay(800).slideDown(800);
$nav.addClass('testnav');
}
if ($(window).scrollTop() < 40) {
$menu.slideUp(800, function () {
$headerdiv.removeClass('testheaderdiv');
});
$nav.removeClass('testnav');
}
});
});
</script>
</body>
</html>
CSS
* {
margin: 0px;
padding: 0px;
color: #00AAFF;
font-family: Arial;
}
body {
height: 800px;
}
header {
position: fixed;
top: 0px;
left: 0px;
height: 100px;
width: 100%;
background-color: #333;
z-index: 1;
}
div#headerdiv {
display: inline;
transition: all 1s;
}
h1 {
display: inline;
margin-left: 40px;
font-size: 40px;
}
header > img#menu {
position: fixed;
top: 20px;
left: 40px;
width: 40px;
height: 40px;
display: none;
}
header > div > img#logo {
display: inline;
width: 60px;
height: 60px;
position: relative;
top: 18px;
left: 20px;
transition: height 1s, width 1s;
}
nav {
position: relative;
top: 100px;
height: 40px;
width: 100%;
background-color: #333;
}
nav > ul {
list-style: none;
}
nav > ul > li {
display: inline-block;
height: 40px;
width: 200px;
text-align: center;
border-right: 1px solid #00AAFF;
}
nav > ul > li > a {
position: relative;
top: 6px;
}
.testheaderdiv {
margin-left: 80px;
transition: all 1s;
}
.testnav {
display: none;
}
The main problem I could see with the code is how scroll is handled, for every scroll event you are adding delay to the menu element.
So try
$(document).ready(function () {
var $menu = $('img#menu');
var $headerdiv = $("div#headerdiv")
var $nav = $('nav');
var flag;
$(window).on('scroll', function () {
if (flag !== 1 && $(window).scrollTop() > 40) {
$headerdiv.addClass("testheaderdiv");
$menu.stop(true, true).delay(800).slideDown(800);
$nav.addClass('testnav');
flag = 1;
}
if (flag !== 2 && $(window).scrollTop() < 40) {
$menu.stop(true, true).slideUp(800, function () {
$headerdiv.removeClass('testheaderdiv');
});
$nav.removeClass('testnav');
flag = 2;
}
});
});