2017-09-27 2 views
0

Ich mache einen Bildregler, der Thumbnails und eine vorherige und nächste Schaltfläche hat. Wenn Sie auf ein Miniaturbild klicken, das sich vom Hauptbild unterscheidet, möchte ich, dass es in das neue Bild eingeblendet wird. Gleiches gilt für die vorherigen und nächsten Schaltflächen.Fade Bilder Schieberegler klicken Zurück Weiter Button

Ich habe eine Fade-Web-Kit-Animation an Ort und Stelle, aber es funktioniert nicht. Hier ist die codepen.

$('#imgDetail li img').click(function(){ 
 
    $('#unidoor').attr('src',$(this).attr('src')); 
 
}); 
 
$('#next').on('click',function(){ 
 
var imgSrc = $('#unidoor').attr('src'); 
 
    var nextSrc = $('ul img[src="'+imgSrc+'"]').closest('li').next().find('img').attr('src'); 
 
    console.log(nextSrc); 
 
    nextSrc ==undefined?$('#unidoor').attr('src',$('ul img:first').attr('src')): $('#unidoor').attr('src',nextSrc); 
 
}); 
 
$('#prev').on('click',function(){ 
 
var imgSrc = $('#unidoor').attr('src'); 
 
    var nextSrc = $('ul img[src="'+imgSrc+'"]').closest('li').prev().find('img').attr('src'); 
 
    console.log(nextSrc); 
 
    nextSrc ==undefined?$('#unidoor').attr('src',$('ul img:last').attr('src')): $('#unidoor').attr('src',nextSrc); 
 
});
* { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
body{ 
 
    margin: 0; 
 
    padding:0; 
 
    font-size: 100%; 
 
/* line-height: 1.6; */ 
 
/* font-family: Arial, Helvetica, sans-serif; */ 
 
} 
 

 
.header{ 
 
    margin: 0 auto; 
 
    width: 100%; 
 
    background-color: #333; 
 
    padding: 30px 0 0 0; 
 
} 
 

 
.header h1{ 
 
    margin: 0; 
 
    text-align: center; 
 
    color: white; 
 
    font-family: Arial, Helvetica, sans-serif; 
 
} 
 

 
.header ul { 
 
    list-style-type: none; 
 
    margin: 0; 
 
/* padding: 0; */ 
 
    overflow: hidden; 
 
    padding: 20px 0px 30px 0; 
 
    text-align: center; 
 
} 
 

 
.header li { 
 
    display: block; 
 
    display: inline-block; 
 
/* border-right: 1px solid #bbb; */ 
 
    border-right: 1px solid #bbb; 
 
    height: 25px; 
 
} 
 

 
.header li:last-child{ 
 
    border-right: none; 
 
} 
 

 
.header li a { 
 
    display: block; 
 
    color: white; 
 
    text-align: center; 
 
    text-decoration: none; 
 
    padding: 0px 40px; 
 
    font-size: 1em; 
 
} 
 

 
.header li a:hover{ 
 
    color: #7bbe9a; 
 
/* color: #80b198; */ 
 
} 
 

 
#green-room { 
 
    background: #333 !important; 
 
} 
 

 
.slideshow-container { 
 
    max-width: 1000px; 
 
    position: relative; 
 
    margin: auto; 
 
} 
 

 
#unidoor { 
 
/* position: relative; */ 
 
    width: 90%; 
 
    margin: 0 auto; 
 
    display: block; 
 
} 
 

 
#prev { 
 
    position: absolute; 
 
    float: left; 
 
    bottom: 55%; 
 
    left: 5%; 
 
    cursor: pointer; 
 
    font-weight: bold; 
 
} 
 

 
#next { 
 
    position: absolute; 
 
    float: right; 
 
    bottom: 55%; 
 
    right: 5%; 
 
    cursor: pointer; 
 
    font-weight: bold; 
 
    width: auto; 
 
} 
 

 
.previous { 
 
    background-color: #fff; 
 
    opacity: 0.5; 
 
    color: black; 
 
    width: auto; 
 
} 
 

 
.next { 
 
    background-color: #fff; 
 
    opacity: 0.5; 
 
    color: black; 
 
} 
 

 
#imgDetail a { 
 
    text-decoration: none; 
 
    display: inline-block; 
 
    padding: 8px 16px; 
 
} 
 

 
#imgDetail a:hover { 
 
    background-color: #7bbe9a; 
 
    color: white; 
 
    opacity: 1; 
 
} 
 

 
#imgDetail ul { 
 
    margin: 0 auto; 
 
    display: block; 
 
    width: 50%; 
 
} 
 

 
/* fade animation */ 
 

 
#fade { 
 
    -webkit-animation-name: fade; 
 
    -webkit-animation-duration: 1.5s; 
 
    animation-name: fade; 
 
    animation-duration: 1.5s; 
 
} 
 

 
@-webkit-keyframes fade { 
 
    from {opacity: .4} 
 
    to {opacity: 1} 
 
} 
 

 
@keyframes fade { 
 
    from {opacity: .4} 
 
    to {opacity: 1} 
 
} 
 

 
.thumb { 
 
    width: 25%; 
 
    height: auto; 
 
    margin: 15px 5px 0 5px; 
 
} 
 

 
#imgDetail li { 
 
    display: inline; margin-right: 10px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!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"/> 
 
    </head> 
 

 
    <body id="green-room"> 
 
    \t <div class="header"> 
 
     <div id="title"><h1>Lorem Ipsum 3D Online Portfolio</h1></div> 
 
     <nav id="menu"> 
 
     <ul> 
 
      <li><a href="index.html">Home</a></li> 
 
      <li><a href="index.html#portfolio">Portfolio</a></li> 
 
      <li><a href="#about">About</a></li> 
 
      <li><a href="#contact">Contact</a></li> 
 
     </ul> 
 
     </nav> 
 
    </div> 
 
    
 
<div class="slideshow-container"> 
 
<div id="imgDetail"> 
 
    <br> 
 
    <img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_1.jpg" alt="" id="unidoor" /> 
 
    <a href="#" id="prev" class="prev-next-button previous">&#8249;</a> 
 
<a href="#" id="next" class="prev-next-button next">&#8250;</a> 
 
    
 
    <ul> 
 
     <li><img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_1.jpg" id="fade" class="thumb" /></li> 
 
     <li><img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_2.jpg" id="fade" class="thumb" /></li> 
 
     <li><img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_3.jpg" id="fade" class="thumb" /></li> 
 
    </ul> 
 
</div> 
 
    </div> 
 
    
 
    <script src="https://unpkg.com/scrollreveal/dist/scrollreveal.min.js"></script> 
 
    <script> 
 
     window.sr = ScrollReveal({reset: true}); 
 
     sr.reveal('#unidoor'); 
 
    </script> 
 
    
 
    </body> 
 

 
</html>

Antwort

0

In Ihrem Ansatz, den Sie einige Code ausführen müssen, habe ich einige Änderungen in Ihrem Code Sie es in dem Codebeispiel sehen können.

Zuerst müssen Sie das Vorschaubild basierend auf den Bildern erstellen, die Sie verschieben müssen.

Zweitens sollten Sie die Eigenschaft src jedes Bildes nicht auf die nächste src (Bild-URL) setzen, da Sie jedes Mal, wenn Sie die Bilder herunterladen, alle Bilder im Cache behalten und sie einmal herunterladen sollten.

Drittens sollten Sie das onclick-Ereignis für Pfeil previous und next in nur einer Funktion hören, vielleicht ist es besser, sie in zwei Funktionen zu trennen, um die einzige Verantwortung für nächste und vorherige Tasten zu vergeben.

Viertens sollten Sie nur CSS-Klasse verwenden, um den Fade-Effekt für jede Folie zu machen, sind weniger komplex thant Animationen, in dem Demo-Code Ich habe die active Klasse, um es als Fade-Effekt funktioniert

Ich hoffe, Sie können es hilfreich finden.

$(document).ready(function(){ 
 
\t // get all images loaded 
 
    var images = $(".unidoor-class"); 
 
\t // thumbnails containers 
 
    var thumbnailContainer = $("#thumbnails"); 
 
    // generate thumbnail images 
 
    generateThumbnails(images, thumbnailContainer); 
 
    // listeners for controls arrows 
 
\t $(".prev-next-button").on("click", function() { 
 
    \t // get the images 
 
    var currentImageIndex = $(".unidoor-class.active").index(); 
 
\t \t var isPrevious = $(this).hasClass("previous"); 
 
    var nextIndex; 
 
    if (isPrevious) { 
 
    \t if (currentImageIndex === 0) { 
 
     \t nextIndex = images.length - 1; 
 
     } 
 
     
 
     if (currentImageIndex > 0) { 
 
     \t nextIndex = currentImageIndex - 1; 
 
     } 
 
    } else { 
 
    \t if (currentImageIndex === images.length - 1) { 
 
     \t nextIndex = 0; 
 
     } 
 
     
 
     if (currentImageIndex < images.length - 1) { 
 
     \t nextIndex = currentImageIndex + 1; 
 
     } 
 
    } 
 
\t \t 
 
    // remove any active class from images 
 
\t \t images.removeClass("active"); 
 
    // get the next active image and add active class to that next current image 
 
    $(images[nextIndex]).addClass("active"); 
 
    }); 
 
    
 
    $(".thumb").on("click", function(event){ 
 
\t event.preventDefault(); 
 
    var indexSelected = $(this).data("img-index"); 
 
    var currentShown = $(".unidoor-class.active").index(); 
 
    if (currentShown === indexSelected) return false; 
 
    images.removeClass("active"); 
 
    $(images[indexSelected]).addClass('active'); 
 
    }); 
 
    
 
    function generateThumbnails(images, container) { 
 
    \t var ul = $("<ul>"); 
 
    \t images.each(function(index, element){ 
 
    \t var currentThumb = $("<img>"); 
 
     var li = $("<li>"); 
 
     var src = $(this).attr("src"); 
 
     currentThumb.attr("src", src); 
 
     currentThumb.attr("class", "thumb"); 
 
     currentThumb.data("img-index", index); 
 
     li.append(currentThumb); 
 
     ul.append(li); 
 
    }); 
 
    container.append(ul); 
 
    } 
 
});
* { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
body{ 
 
    margin: 0; 
 
    padding:0; 
 
    font-size: 100%; 
 
/* line-height: 1.6; */ 
 
/* font-family: Arial, Helvetica, sans-serif; */ 
 
} 
 

 
.header{ 
 
    margin: 0 auto; 
 
    width: 100%; 
 
    background-color: #333; 
 
    padding: 30px 0 0 0; 
 
} 
 

 
.header h1{ 
 
    margin: 0; 
 
    text-align: center; 
 
    color: white; 
 
    font-family: Arial, Helvetica, sans-serif; 
 
} 
 

 
.header ul { 
 
    list-style-type: none; 
 
    margin: 0; 
 
/* padding: 0; */ 
 
    overflow: hidden; 
 
    padding: 20px 0px 30px 0; 
 
    text-align: center; 
 
} 
 

 
.header li { 
 
    display: block; 
 
    display: inline-block; 
 
/* border-right: 1px solid #bbb; */ 
 
    border-right: 1px solid #bbb; 
 
    height: 25px; 
 
} 
 

 
.header li:last-child{ 
 
    border-right: none; 
 
} 
 

 
.header li a { 
 
    display: block; 
 
    color: white; 
 
    text-align: center; 
 
    text-decoration: none; 
 
    padding: 0px 40px; 
 
    font-size: 1em; 
 
} 
 

 
.header li a:hover{ 
 
    color: #7bbe9a; 
 
/* color: #80b198; */ 
 
} 
 

 
#green-room { 
 
    background: #333 !important; 
 
} 
 

 
.slideshow-container { 
 
    max-width: 1000px; 
 
    position: relative; 
 
    margin: auto; 
 
} 
 

 
#unidoor, .unidoor-class { 
 
    position: absolute; 
 
    width: 100%; 
 
    margin: 0 auto; 
 
    display: block; 
 
    top: 0; 
 
    left: 0; 
 
    opacity: 0; 
 
    transition: opacity .5s; 
 
} 
 

 
.unidoor-class.active { 
 
    position: relative; 
 
    opacity: 1; 
 
} 
 

 
#prev { 
 
    position: absolute; 
 
    float: left; 
 
    bottom: 55%; 
 
    left: 5%; 
 
    cursor: pointer; 
 
    font-weight: bold; 
 
} 
 

 
#next { 
 
    position: absolute; 
 
    float: right; 
 
    bottom: 55%; 
 
    right: 5%; 
 
    cursor: pointer; 
 
    font-weight: bold; 
 
    width: auto; 
 
} 
 

 
.previous { 
 
    background-color: #fff; 
 
    opacity: 0.5; 
 
    color: black; 
 
    width: auto; 
 
} 
 

 
.next { 
 
    background-color: #fff; 
 
    opacity: 0.5; 
 
    color: black; 
 
} 
 

 
#imgDetail { 
 
    position: relative; 
 
    width: 90%; 
 
    margin: 0 auto; 
 
} 
 

 
#imgDetail a { 
 
    text-decoration: none; 
 
    display: inline-block; 
 
    padding: 8px 16px; 
 
} 
 

 
#imgDetail a:hover { 
 
    background-color: #7bbe9a; 
 
    color: white; 
 
    opacity: 1; 
 
} 
 

 
#imgDetail ul { 
 
    margin: 0 auto; 
 
    display: block; 
 
    width: 50%; 
 
} 
 

 
/* fade animation */ 
 

 
#fade { 
 
    -webkit-animation-name: fade; 
 
    -webkit-animation-duration: 1.5s; 
 
    animation-name: fade; 
 
    animation-duration: 1.5s; 
 
} 
 

 
@-webkit-keyframes fade { 
 
    from {opacity: .4} 
 
    to {opacity: 1} 
 
} 
 

 
@keyframes fade { 
 
    from {opacity: .4} 
 
    to {opacity: 1} 
 
} 
 

 
.thumb { 
 
    width: 25%; 
 
    height: auto; 
 
    margin: 15px 5px 0 5px; 
 
} 
 

 
#imgDetail li { 
 
    display: inline; margin-right: 10px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<div class="slideshow-container"> 
 
    <div id="imgDetail"> 
 
    <img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_1.jpg" class="unidoor-class active" /> 
 
    <img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_2.jpg" class="unidoor-class" /> 
 
    <img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_3.jpg" class="unidoor-class" /> 
 
    <!--CONTROLS--> 
 
    <a href="#" id="prev" class="prev-next-button previous">&#8249;</a> 
 
    <a href="#" id="next" class="prev-next-button next">&#8250;</a> 
 
    <!--Thumbnails--> 
 
    <div id="thumbnails"> 
 
    </div> 
 
    </div> 
 
</div>

+0

Danke, das hat mir sehr geholfen. – user7311741

+0

Ich bin froh zu sehen, dass Sie willkommen sind: D –