jquery - 向 jQuery 代码添加事件监听器

标签 jquery html css event-handling

我正在使用一些 jQuery 来实现轮播图片 slider ,它有两个按钮:下一张图片和上一张图片。我如何向两个按钮添加鼠标单击事件监听器,以便在单击任一按钮时在 slider 下方打印相应的文本,从而生成图片幻灯片,其中每张图片都与一些文本相关联?任何见解将不胜感激。

<!DOCTYPE html>
  <html>
  <head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
  jQuery(document).ready(function ($) {

  $('#checkbox').change(function(){
    setInterval(function () {
    moveRight();
   }, 3000);
 });

var slideCount = $('#slider ul li').length;
var slideWidth = $('#slider ul li').width();
var slideHeight = $('#slider ul li').height();
var sliderUlWidth = slideCount * slideWidth;

$('#slider').css({ width: slideWidth, height: slideHeight });

$('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });

$('#slider ul li:last-child').prependTo('#slider ul');

function moveLeft() {
    $('#slider ul').animate({
        left: + slideWidth
    }, 200, function () {
        $('#slider ul li:last-child').prependTo('#slider ul');
        $('#slider ul').css('left', '');
    });
};

function moveRight() {
    $('#slider ul').animate({
        left: - slideWidth
    }, 200, function () {
        $('#slider ul li:first-child').appendTo('#slider ul');
        $('#slider ul').css('left', '');
    });
};

$('button.control_prev').click(function () {
    moveLeft();
});

$('button.control_next').click(function () {
    moveRight();
});

});  

</script>
<style>

@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,600);  

nav { 
float: left;
background-color: #EEEFFF;
height: 1500px;
width: 100px;
}

html {
border-top: 5px solid #fff;
background: #58DDAF;
color: #2a2a2a;
}

html, body {
margin: 0;
padding: 0;
font-family: 'Open Sans';
}

h1 {
color: #fff;
text-align: center;
font-weight: 300;
}

#slider {
position: relative;
overflow: hidden;
margin: 20px auto 0 auto;
border-radius: 4px;
}

#slider ul {
position: relative;
margin: 0;
padding: 0;
height: 200px;
list-style: none;
}

#slider ul li {
position: relative;
display: block;
float: left;
margin: 0;
padding: 0;
width: 500px;
height: 300px;
background: #ccc;
text-align: center;
line-height: 300px;
}

button.control_prev, button.control_next {
position: absolute;
top: 40%;
z-index: 999;
display: block;
padding: 4% 3%;
width: auto;
height: auto;
background: #2a2a2a;
color: #fff;
text-decoration: none;
font-weight: 600;
font-size: 18px;
opacity: 0.8;
cursor: pointer;
border:solid transparent;
}

button.control_prev:hover, button.control_next:hover {
opacity: 1;
-webkit-transition: all 0.2s ease;
}

button.control_prev {
border-radius: 0 2px 2px 0;
}

button.control_next {
right: 0;
border-radius: 2px 0 0 2px;
}

.slider_option {
position: relative;
margin: 10px auto;
width: 160px;
font-size: 18px;
}

</style>
</head>

<body>
<nav>
test
</nav>
<div id="slider">
  <button class="control_next"></button>
  <button class="control_prev"></button>
    <ul>
     <li>SLIDE 1</li>
     <li style="background: #aaa;">SLIDE 2</li>
     <li>SLIDE 3</li>
     <li style="background: #aaa;">SLIDE 4</li>
    </ul>  
</div>
</body>
</html>

最佳答案

我对您的以下 2 个函数做了细微的更改:

DEMO

function moveLeft() {
    $('#slider ul').animate({
        left: + slideWidth
    }, 200, function () {
        $('#slider ul li:last-child').prependTo('#slider ul');
        $('#slider ul').css('left', '');

    });
    var current=$("li.active"); //get the current active li
    var prev=current.prev('li'); //get its prev li
    current.removeClass('active'); //remove active from current li
    if(prev.length==0) //if no previous element is present
        $('li:last').addClass('active'); //make last li as active
    else
        prev.addClass('active'); //make prev li active
    var cur=$('li.active').attr('data-des'); once active is set to prev or next li get its data-des attribute
    $('.imageDes').fadeOut('500').text(cur).fadeIn('500'); //set it to a div

};

function moveRight() {
    $('#slider ul').animate({
        left: - slideWidth
    }, 200, function () {
        $('#slider ul li:first-child').appendTo('#slider ul');
        $('#slider ul').css('left', '');
    });
    var current=$("li.active");
    var next=current.next('li');
    current.removeClass('active');
    if(next.length==0)
        $('li:first').addClass('active');
    else
        next.addClass('active');
    var cur=$('li.active').attr('data-des');
    $('.imageDes').fadeOut('500').text(cur).fadeIn('500');
};

请参阅 moveLeft() 的行注释,它与 moveRight() 相同。

你的 html:

<div class="imageDes">Image 1</div><!--this is where you display your corresponding text-->
<button class="control_next"></button>
<button class="control_prev"></button>
<ul>
   <!--Added extra attribute to each li with name data-des-->
   <li class="active" data-des="Image 1">SLIDE 1</li> 
   <li data-des="Image 2" style="background: #aaa;">SLIDE 2</li>
   <li data-des="Image 3">SLIDE 3</li>
   <li data-des="Image 4" style="background: #aaa;">SLIDE 4</li>
</ul>  

关于jquery - 向 jQuery 代码添加事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31577913/

相关文章:

css - 悬停在下拉菜单项上时父菜单项的事件状态

css - 如何构建 Bootstrap 4 并牢记桌面优先方法

javascript - 垂直添加元素时如何防止背景图像在移动浏览器上缩放?

jquery - 我怎样才能在背景中看到完整的图像?

jQuery 是与 Web 表单还是 HTML 表单一起使用?

php - 如何正确格式化从网站检索的文本?

javascript - onclick ="location.href=' link.html'"不起作用

javascript - jquery获取选中的复选框行并将其传递到ajax中

jquery - 如何旋转文本,尤其是在 ie8 中?

javascript - 我可以自动提示用户将移动网络应用程序添加到他们的主屏幕吗? [iOS]