I am new to jQuery and I tried to create my own slider that would
- automatically moves to a specified time interval
- Slide when clicking on brand navigation
- The active state of navigation automatically changes with an automatic slide
- The active link changes when the user clicks on the navigation through the brand.
The code I tried is below. Sorry if this is not straightforward and simple, as I am new to this. The part I commented on in jQuery is for auto slip.
Problem. When I uncomment the auto-exchange code, bullet navigation does not work and vice versa. Only one works at a time. Please help me with this. Thanks.
https://jsfiddle.net/Kowshikan/ac46y9do/
HTML:
<div id="newsFeed">
<h3> Our Latest News </h3>
<ul id="slideBox">
<li id="1">
Content 1
</li>
<li id="2">
Content 2
</li>
<li id="3">
Content 3
</li>
<li id="4">
Content 4
</li>
<li id="5">
Content 5
</li>
<div class="clearfix"> </div>
</ul>
<div class="pager">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<div class="clearfix"> </div>
</div>
</div>
CSS
#newsFeed
{
overflow: hidden;
}
#slideBox
{
overflow: hidden;
padding: 0;
position: relative;
margin-bottom: 0;
}
#slideBox li
{
border: 1px solid #ccc;
list-style-type: none;
float: left;
padding: 10px;
min-height: 336px;
}
.pager
{
margin: 0;
text-align: left;
background-color: #F5F5F5;
min-height: 65px;
border: 1px solid #ccc;
border-top: 0 !important;
position: relative;
}
.pager ul
{
padding: 10px;
}
.pager a
{
color: #444 !important;
font-weight: bold;
font-size: 11px;
display: block;
padding: 5px 10px;
background: linear-gradient(center top , #FFF 0%, #F0F0F0 100%) repeat scroll 0% 0% transparent;
background: -webkit-linear-gradient(center top , #FFF 0%, #F0F0F0 100%) repeat scroll 0% 0% transparent;
background: -moz-linear-gradient(center top , #FFF 0%, #F0F0F0 100%) repeat scroll 0% 0% transparent;
border: 1px solid #ccc;
text-align: center;
position: absolute;
right: 10px;
bottom: 10px;
}
.pager a:hover
{
text-decoration: none;
}
.pagerActive
{
background: linear-gradient(center top , #eeeeee 0%, #ffffff 100%) repeat scroll 0 0 rgba(0, 0, 0, 0) !important;
background: -webkit-linear-gradient(center top , #eeeeee 0%, #ffffff 100%) repeat scroll 0 0 rgba(0, 0, 0, 0) !important;
background: -moz-linear-gradient(center top , #eeeeee 0%, #ffffff 100%) repeat scroll 0 0 rgba(0, 0, 0, 0) !important;
border: 1px solid #bbbbbb !important;
}
.pager li
{
cursor: pointer;
color: #777;
width: 30px;
height: 30px;
border: 1px solid #ccc;
background: linear-gradient(center top , #FFF 0%, #EEE 100%) repeat scroll 0% 0% transparent;
background: -webkit-linear-gradient(center top , #FFF 0%, #EEE 100%) repeat scroll 0% 0% transparent;
background: -moz-linear-gradient(center top , #FFF 0%, #EEE 100%) repeat scroll 0% 0% transparent;
display: inline-block;
text-align: center;
vertical-align: middle;
padding: 3px;
}
.pager li:hover
{
border: 1px solid #aaaaaa;
}
Jquery:
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
totalslide = $('#slideBox li').length;
slidewidth = $('#newsFeed').width();
var totalwidth = slidewidth * totalslide;
$('#slideBox').css({'width': totalwidth});
$('#slideBox li').css({'width': slidewidth});
$('.pager li:first').addClass('pagerActive');
$('.pager li').click(function(){
var slide = ($(this).html() - 1) * slidewidth;
$('#slideBox').animate({right: slide}, 500);
$('.pager li').removeClass('pagerActive');
$(this).addClass('pagerActive');
});
});
</script>