Afton
Afton

Reputation: 583

jQuery image slider not working with jQuery-cycle (ASP.NET)

I am working on implementing an Obligatory image slider with jQuery on my ASP.NET web app. I can see the first picture, but when I press the next or previous buttons, it doesn't work.

I installed the jQuery.cycle plug in as well, but don't know what's wrong. Do I need another plug-in along with jQuery cycle?

Html

<div id="slider-nav">

        <div id="next">&rang;</div>
        <div id="prev">&lang;</div>

        <div id="slider">
            <img src="~/img/img1.jpg" alt="image">
            <img src="~/img/img2.jpg" alt="image">
            <img src="~/img/img3.jpg" alt="image">
        </div>
    </div>

CSS

<style type="text/css">

    #slider-nav{
        width: 700px;
        height: 280px;
        position: relative;
        margin: 50px auto;
    }

    #slider{
        width: 700px;
        height: 280px;
        position: absolute;
        overflow: hidden;
    }

    #next {
        text-align: center;
        line-height: 50px;
        color: white;
        width: 50px;
        height: 50px;
        background-color: black;
        position: absolute;
        top: 120px;
        right: 0;
        z-index: 99;
        cursor: pointer;
        opacity: 0;
    }

    #prev {
        text-align: center;
        line-height: 50px;
        color: white;
        width: 50px;
        height: 50px;
        background-color: black;
        position: absolute;
        top: 120px;
        left: 0;
        z-index: 99;
        cursor: pointer;
        opacity: 0;
    }

    #slider-nav:hover #next {
        opacity: 1;
        transition: all .5s ease-out;
        -webkit-transition: all .5s ease-out;
    }

    #slider-nav:hover #prev {
        opacity: 1;
        transition: all .5s ease-out;
        -webkit-transition: all .5s ease-out;
    }
  </style>

Javascript

<script type="text/javascript" src="~/Scripts/jquery-3.1.1.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.cycle.all.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.cycle.all.min.js"></script>

<script type="text/javascript">

    $('#slider').cycle({
        fx: 'scrollHorz',
        next: '#next',
        prev: '#prev',
        timeout: 3000,
        pause: 1
    });
</script>

Upvotes: 0

Views: 118

Answers (1)

Marcus
Marcus

Reputation: 453

Remove the duplicate <script type="text/javascript" src="~/Scripts/jquery.cycle.all.min.js"></script>

You already have it called (.min means minified version of the script.)

And wrap your call in a ready block

<script type="text/javascript">
$( document ).ready(function() {
    $('#slider').cycle({
        fx: 'scrollHorz',
        next: '#next',
        prev: '#prev',
        timeout: 3000,
        pause: 1
    });
});
</script>

UPDATE: I created a snippet using your exact code. it works perfectly. this indicates that your other scripts are interfering.

    $('#slider').cycle({
        fx: 'scrollHorz',
        next: '#next',
        prev: '#prev',
        timeout: 3000,
        pause: 1
    });
    #slider-nav{
        width: 700px;
        height: 280px;
        position: relative;
        margin: 50px auto;
    }

    #slider{
        width: 700px;
        height: 280px;
        position: absolute;
        overflow: hidden;
    }

    #next {
        text-align: center;
        line-height: 50px;
        color: white;
        width: 50px;
        height: 50px;
        background-color: black;
        position: absolute;
        top: 120px;
        right: 0;
        z-index: 99;
        cursor: pointer;
        opacity: 0;
    }

    #prev {
        text-align: center;
        line-height: 50px;
        color: white;
        width: 50px;
        height: 50px;
        background-color: black;
        position: absolute;
        top: 120px;
        left: 0;
        z-index: 99;
        cursor: pointer;
        opacity: 0;
    }

    #slider-nav:hover #next {
        opacity: 1;
        transition: all .5s ease-out;
        -webkit-transition: all .5s ease-out;
    }

    #slider-nav:hover #prev {
        opacity: 1;
        transition: all .5s ease-out;
        -webkit-transition: all .5s ease-out;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://malsup.github.com/jquery.cycle.all.js"></script>

<div id="slider-nav">

        <div id="next">&rang;</div>
        <div id="prev">&lang;</div>

        <div id="slider">
            <img src="//placehold.it/250x250/ff00ff" alt="image">
            <img src="//placehold.it/250x250/0000ff" alt="image">
            <img src="//placehold.it/250x250/00ff00" alt="image">
        </div>
    </div>

Upvotes: 1

Related Questions