5

I am implementing carousel through plain javascript (use no plugin). I want to set prev and next button to control slide image.

var firstval = 0;

function Carousel() {
    firstval += 2;
    parent = document.getElementById('container-carousel');
    parent.style.left = "-" + firstval + "px";
    if (!(firstval % 150)) {
        setTimeout(Carousel, 3000);
        firstval = 0;
        var firstChild = parent.firstElementChild;
        parent.appendChild(firstChild);
        parent.style.left= 0;
        return;
    }
    runCarousel = setTimeout(Carousel, 10);
}
Carousel();
        #wrapper-carousel {
            position: relative;
            width: 450px;
            height: 150px;
            margin: 0 auto;
            overflow: hidden;
        }
        #container-carousel {
            position: absolute;
            width: 450px;
            height: 150px;
        }
        .child {
            width: 150px;
            height: 150px;
            padding-top: 35px;
            float: left;
            text-align: center;
            font-size: 60px;
        }
        
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div id="wrapper-carousel">
    <div id="container-carousel">
        <div  class="child"><img width="100" src="https://d2z4fd79oscvvx.cloudfront.net/0020232_chocolate_cream_gateaux_cake_320.jpeg"> </div>
        <div  class="child"><img width="100" src="https://d2z4fd79oscvvx.cloudfront.net/0018904_50_red_roses_in_vase_320.jpeg"> </div>
        <div  class="child"><img width="100" src="https://d2z4fd79oscvvx.cloudfront.net/0020232_chocolate_cream_gateaux_cake_320.jpeg"> </div>
    </div>
      <a class="left" href="#wrapper-carousel"  style="font-size:100px;z-index:3000;">&lsaquo;</a>
                                        <a class="right" href="#wrapper-carousel"  style="font-size:100px;z-index:3000">&rsaquo;</a>
 
</div>

I want to add simply button to control this carousel. I am not using any plugin and any framework carousel.

JsFiddle: https://jsfiddle.net/varunPes/wzkLjh8s/21/

4
  • can you say your problem bit clear. you want to do css or functionality Commented May 10, 2016 at 7:17
  • Have you tried any thing on left and right click? Commented May 10, 2016 at 7:18
  • @ketan. i tried href="#wrapper-carousel " , Commented May 10, 2016 at 7:20
  • @varunsharma You can move ahead here.jsfiddle.net/wzkLjh8s/24 Commented May 10, 2016 at 7:30

2 Answers 2

2

Carousel without Plugin.

var firstval = 0;
var runSlider;

function Carousel() {
clearTimeout(runSlider);
    firstval += 2;
    parent = document.getElementById('container-carousel');
    parent.style.left = "-" + firstval + "px";
    if (!(firstval % 130)) {
        setTimeout(Carousel, 3000);
        firstval = 0;
        var firstChild = parent.firstElementChild;
        parent.appendChild(firstChild);
        parent.style.left= 0;
        return;
    }
    runCarousel = setTimeout(Carousel, 10);
}
Carousel();


function leftClick(){
firstval += 2;
    parent = document.getElementById('container-carousel');
    parent.style.left = "-" + firstval + "px";
    
    if (!(firstval % 130)) {
        
        firstval = 0;
        var firstChild = parent.firstElementChild;
        parent.appendChild(firstChild);
        parent.style.left= 0;
        return;
    }
    runSlider = setTimeout(leftClick, 10);
}

function rightClick(){
firstval += 2;
    parent = document.getElementById('container-carousel');
    parent.style.left =  firstval + "px";
    
    if (!(firstval % 130)) {
        
        firstval = 0;
        var firstChild = parent.firstElementChild;
        parent.appendChild(firstChild);
        parent.style.left= 0;
        return;
    }
    runSlider = setTimeout(rightClick, 10);
}
 #wrapper-carousel {
            position: relative;
            width: 450px;
            height: 150px;
            margin: 0 auto;
            overflow: hidden;
            display:flex;
        }
        #main-carousel {
            position: relative;
            width: 450px;
            height: 150px;
            overflow:hidden;
        }
         #container-carousel {
            position: absolute;
            width: 450px;
            height: 150px;
        }
        .child {
            width: 130px;
            height: 150px;
            padding-top: 35px;
            float: left;
            text-align: center;
            font-size: 60px;
        }
        
<div id="wrapper-carousel">
<a class="left" href="#wrapper-carousel"  style="font-size:100px;z-index:3000;" onclick="leftClick()">&lsaquo;</a>
<div id="main-carousel">
    <div id="container-carousel">
        <div  class="child"><img width="100" src="https://d2z4fd79oscvvx.cloudfront.net/0020232_chocolate_cream_gateaux_cake_320.jpeg"> </div>
        <div  class="child"><img width="100" src="https://d2z4fd79oscvvx.cloudfront.net/0018904_50_red_roses_in_vase_320.jpeg"> </div>
        <div  class="child"><img width="100" src="https://d2z4fd79oscvvx.cloudfront.net/0020232_chocolate_cream_gateaux_cake_320.jpeg"> </div>
        </div></div>
  <a class="right" href="#wrapper-carousel"  style="font-size:100px;z-index:3000" onclick="rightClick()">&rsaquo;</a>
 
</div>

Carousel with plain javascript.

Sign up to request clarification or add additional context in comments.

Comments

1

Understood that you want css fix to be done

<div id="wrapper-carousel">
    <div id="container-carousel">
        <div  class="child"><img width="100" src="https://d2z4fd79oscvvx.cloudfront.net/0020232_chocolate_cream_gateaux_cake_320.jpeg"> </div>
        <div  class="child"><img width="100" src="https://d2z4fd79oscvvx.cloudfront.net/0018904_50_red_roses_in_vase_320.jpeg"> </div>
        <div  class="child"><img width="100" src="https://d2z4fd79oscvvx.cloudfront.net/0020232_chocolate_cream_gateaux_cake_320.jpeg"> </div>
    </div>
      <a class="left" href="#wrapper-carousel"  style="font-size: 100px;z-index: 3050;float: left;position: relative;background: #F3F5F6;top:15px">&lsaquo;</a>
     <a class="right" href="#wrapper-carousel"  style="font-size: 100px;z-index: 3050;float: right;position: relative;background: #F3F5F6;top:15px">&rsaquo;</a>

</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.