与 jQuery Toggle 功能作斗争
我正在制作一个乐队简介页面,例如 此处。
这个想法是单击一张照片将在左侧显示乐队成员简介,覆盖整个乐队描述。
我对当前代码的问题是,使用切换来显示替代的单独简介意味着当您在不同成员之间单击时,有时切换状态会重置,并且似乎什么也没有发生。每次您单击照片时,都会显示该人的简介。仅当您单击同一按钮两次或“返回乐队简介”按钮(尚未出现在页面上)时,才会显示主简介。
我当前的代码如下:
jQuery(document).ready(function($) {
$('#gotoben').toggle(function() {
$('li.gotoben').fadeTo("slow", 1);
$("#dan").hide();
$("#ben").show();
$('li.gotoben').siblings().fadeTo("slow", 0.3);
},
function () {
$('li.gotoben').fadeTo("slow", 1);
$('li.gotoben').siblings().fadeTo("slow", 1);
$("#ben, #dan").hide();
});
$('#gotodan').toggle(function() {
$('li.gotodan').fadeTo("slow", 1);
$("#ben").hide();
$("#dan").show();
$('li.gotodan').siblings().fadeTo("slow", 0.3);
},
function () {
$('li.gotodan').fadeTo("slow", 1);
$('li.gotodan').siblings().fadeTo("slow", 1);
$("#dan, #ben").hide();
});
});
我尝试使用 .click 函数,但也无法使一切工作 100% 顺利。
谁能帮我完成这项工作吗?
编辑 - 工作代码
这里添加到 patricks 代码中的唯一内容是包含 jQuery stop
函数。
jQuery(document).ready(function($) {
$('.bigLeft div:gt(0)').hide();
$('ol.band li').click(function() {
var $th = $(this);
// Hide the current profiles
$(".bigLeft").children().hide();
if( $th.hasClass('selected') ) {
$th.stop(true, false).siblings().fadeTo("slow", 1);
$th.removeClass('selected');
$('#theBand').show(); // <-- change the ID to the default
} else {
$th.addClass('selected');
// Grab the ID of the one that was clicked
var id = $th.attr('id');
// Fade in the current, and fade the siblings
$th.stop(true, false).fadeTo("slow", 1)
.siblings().removeClass('selected').stop(true, false).fadeTo("slow", 0.3);
// Show the clicked profile by concatenating the ID clicked with '_profile'
$("#" + id + "_profile").show();
}
});
});
I am working on a band blurb page, example here.
The idea is to click on a photo which will display the band member blurb on the left, overlaying the overall band description.
My issue with the current code is that using toggle to display the alternative individual blurbs means that when you click between the different members, sometimes the toggle state resets and it seems like nothing is happening. Each time you click a photo, that persons bio should display. Only when you click the same one twice, or a 'return to band bio' button (not yet on the page) should the main bio display.
My current code is below:
jQuery(document).ready(function($) {
$('#gotoben').toggle(function() {
$('li.gotoben').fadeTo("slow", 1);
$("#dan").hide();
$("#ben").show();
$('li.gotoben').siblings().fadeTo("slow", 0.3);
},
function () {
$('li.gotoben').fadeTo("slow", 1);
$('li.gotoben').siblings().fadeTo("slow", 1);
$("#ben, #dan").hide();
});
$('#gotodan').toggle(function() {
$('li.gotodan').fadeTo("slow", 1);
$("#ben").hide();
$("#dan").show();
$('li.gotodan').siblings().fadeTo("slow", 0.3);
},
function () {
$('li.gotodan').fadeTo("slow", 1);
$('li.gotodan').siblings().fadeTo("slow", 1);
$("#dan, #ben").hide();
});
});
I have tried using the .click function but also cannot make everything work 100% smoothly.
Can anyone help me make this work?
EDIT - WORKING CODE
The only thing added here to patricks code is the inclusion of the jQuery stop
function.
jQuery(document).ready(function($) {
$('.bigLeft div:gt(0)').hide();
$('ol.band li').click(function() {
var $th = $(this);
// Hide the current profiles
$(".bigLeft").children().hide();
if( $th.hasClass('selected') ) {
$th.stop(true, false).siblings().fadeTo("slow", 1);
$th.removeClass('selected');
$('#theBand').show(); // <-- change the ID to the default
} else {
$th.addClass('selected');
// Grab the ID of the one that was clicked
var id = $th.attr('id');
// Fade in the current, and fade the siblings
$th.stop(true, false).fadeTo("slow", 1)
.siblings().removeClass('selected').stop(true, false).fadeTo("slow", 0.3);
// Show the clicked profile by concatenating the ID clicked with '_profile'
$("#" + id + "_profile").show();
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我倾向于采取一些不同的方法。
为
ol.bind
下的每个li
指定 ID#ben
、#dan
等。然后为每个配置文件指定一个类似的 ID
#ben_profile
、#dan_profile
等。然后利用这种一致性来发挥您的优势:
您可以在这里尝试一下: http://jsfiddle.net/kDqsT/
I would tend to take a little different approach.
Give each
li
underol.bind
the ID#ben
,#dan
, etc.Then give each profile a similar ID
#ben_profile
,#dan_profile
, etc.Then use that consistency to your advantage:
You can try it out here: http://jsfiddle.net/kDqsT/
这是一种使用点击功能来完成此操作的方法。
我确信还有很多比这更好的方法。
Here's a way to do it with the click function.
I'm sure there are any number of better ways than this.