我正在尝试在jQuery中构建一个基本滑块。我有5个主要部分显示在页面上,当您将鼠标悬停在其容器div上时,会在第一个和最后一个元素上弹出箭头。
我的下一步是编写一个代码,用于在数组中存储div(左侧或右侧,具体取决于点击侧),并将其从DOM中删除。但是,我在点击事件时遇到了麻烦。
这是我的css:
.left_arrow_background {
height: 175px;
width: 65px;
/* background:url(../images/left-arrow.png) no-repeat; */
background:#ccc;
opacity: .9;
position: absolute;
cursor: pointer;
z-index: 2;
}
.right_arrow_background {
height: 175px;
width: 65px;
/* background:url(../images/right-arrow.png) no-repeat;*/
background:#ccc;
z-index: 2;
opacity: .9;
position: relative;
left:890px;
cursor: pointer;
}
.boomers_scroller {
height:275px;
padding-left:1px;
display:block;
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: #CCC;
overflow:hidden;
}
.all-boomers {
display: block;
float: left;
height: 225px;
margin-bottom: 20px;
margin-left: 20px;
width: 175px;
}
输出的HTML:
<div class="all-boomers">
<a href="/boomers/profile/id/2"><img src="/images/users/medium_thumbs/4fa44866c3561.jpg" alt=""/></a>
<p class="boomers_name">PC Guy</p>
<p class="activity_name">IT Guy PC Repair</p>
</div>
<!-- three more identical section here -->
<div class="all-boomers">
<a href="/boomers/profile/id/6"><img src="/images/users/medium_thumbs/4fa62dff64c44.jpg" alt=""/></a>
<p class="boomers_name">Cruella Deville</p>
<p class="activity_name">Dalmation Dog-Sitter</p>
</div>
即使从样式表中删除z-index
,我似乎也无法记录点击事件。到目前为止,这是我的js文件:
$(document).ready(function() {
var boomers = $('.all-boomers');
var boomersLength = boomers.length;
var boomersContainer = {};
$(".all-boomers:eq(4)").attr('id','right-boomer');
$(".all-boomers:eq(0)").removeClass("all-boomers").addClass("one-fifth first").attr('id','left-boomer');
var image_left = '<div class="left_arrow_background"></div>';
var image_right = '<div class="right_arrow_background"></div>';
$(".boomers_scroller").hover(
function(){
$(image_left).insertBefore('#left-boomer');
$(image_right).insertAfter('#right-boomer');
},
function(){
$('.left_arrow_background').remove();
$('.right_arrow_background').remove();
});
$("div.left_arrow_background").click(function(){
var left_boomer = $('#left-boomer').children();
// store left_boomer in boomerscontainer, then remove it from dom
console.log('clicked!');
});
});
这不只是点击事件......我也尝试了其他鼠标事件,没有任何事情触发控制台测试消息,或者如果我将代码置于.click函数中,它将不会触发代码。
编辑:添加了第4个(相关的CSS样式)
答案 0 :(得分:0)
你忘了关闭ready()函数(或者在粘贴之前复制它:) 再加一个});在这段代码的底部。 这是孤立的: http://jsfiddle.net/Q3rDz/
答案 1 :(得分:-1)
固定!当我点击时,悬停事件仍然存在,因此将click事件嵌套在悬停事件中会清除问题。
$(document).ready(function() {
var boomers = $('.all-boomers');
var boomersLength = boomers.length;
var boomersContainer = {};
$(".all-boomers:eq(4)").attr('id','right-boomer');
$(".all-boomers:eq(0)").removeClass("all-boomers").addClass("one-fifth first").attr('id','left-boomer');
var image_left = '<div class="left_arrow_background"></div>';
var image_right = '<div class="right_arrow_background"></div>';
$(".boomers_scroller").hover( function(){
$(image_left).insertBefore('#left-boomer');
$(image_right).insertAfter('#right-boomer');
$("div.left_arrow_background").click(function(){
var left_boomer = $('#left-boomer').children();
// store left_boomer in boomerscontainer, then remove it from dom
console.log('clicked!'); });
},
function(){
$('.left_arrow_background').remove();
$('.right_arrow_background').remove();
});
});