Has*_*shi 87 javascript jquery drupal javascript-framework content-management-system
我有一个在Drupal上开发的网站.我使用一个名为collapsiblock的模块(它基本上是一个JQuery插件)来实现类似手风琴的效果.它对我很好(虽然它是在Beta).但我想修改它,以便当用户点击手风琴的一个项目时,其他项目将崩溃.
在其当前的统计数据中,它的工作方式是,当用户点击一个项目时,它将检查项目是否已折叠或展开,并且它将使项目相反.这意味着如果用户点击一个项目,它将展开,如果他/她点击另一个项目,它也将展开,但它不会折叠先前点击的项目.
你可以看到下面的代码.我知道在哪里可以添加要折叠的代码以及如何折叠和展开.我的问题是:如何选择所有具有".collapsiblock"类的项目,除了用户点击的项目?
注意:具有".collapsiblockCollapsed"类的项目将被折叠,如果从项目中删除此类,则会扩展该项目.
// $Id: collapsiblock.js,v 1.6 2010/08/18 19:17:37 gagarine Exp $
Drupal.Collapsiblock = Drupal.Collapsiblock || {};
Drupal.behaviors.collapsiblock = function (context) {
var cookieData = Drupal.Collapsiblock.getCookieData();
var slidetype = Drupal.settings.collapsiblock.slide_type;
var defaultState = Drupal.settings.collapsiblock.default_state;
var slidespeed = parseInt(Drupal.settings.collapsiblock.slide_speed);
$('div.block:not(.collapsiblock-processed)', context).addClass('collapsiblock-processed').each(function () {
var id = this.id;
var titleElt = $(':header:first', this).not($('.content :header',this));
if (titleElt.size()) {
titleElt = titleElt[0];
// Status values: 1 = not collapsible, 2 = collapsible and expanded, 3 = collapsible and collapsed, 4 = always collapsed
var stat = Drupal.settings.collapsiblock.blocks[this.id] ? Drupal.settings.collapsiblock.blocks[this.id] : defaultState;
if (stat == 1) {
return;
}
titleElt.target = $(this).find('div.content');
$(titleElt)
.addClass('collapsiblock')
.click(function () {
var st = Drupal.Collapsiblock.getCookieData();
if ($(this).is('.collapsiblockCollapsed')) {
$(this).removeClass('collapsiblockCollapsed');
if (slidetype == 1) {
$(this.target).slideDown(slidespeed);
}
else {
$(this.target).animate({height:'show', opacity:'show'}, slidespeed);
}
// Don't save cookie data if the block is always collapsed.
if (stat != 4) {
st[id] = 1;
}
}
else {
$(this).addClass('collapsiblockCollapsed');
if (slidetype == 1) {
$(this.target).slideUp(slidespeed);
}
else {
$(this.target).animate({height:'hide', opacity:'hide'}, slidespeed);
}
// Don't save cookie data if the block is always collapsed.
if (stat != 4) {
st[id] = 0;
}
}
// Stringify the object in JSON format for saving in the cookie.
var cookieString = '{ ';
var cookieParts = [];
$.each(st, function (id, setting) {
cookieParts[cookieParts.length] = ' "' + id + '": ' + setting;
});
cookieString += cookieParts.join(', ') + ' }';
$.cookie('collapsiblock', cookieString, {path: Drupal.settings.basePath});
});
// Leave active blocks uncollapsed. If the block is expanded, do nothing.
if (stat == 4 || (cookieData[id] == 0 || (stat == 3 && cookieData[id] == undefined)) && !$(this).find('a.active').size()) {
$(titleElt).addClass('collapsiblockCollapsed');
$(titleElt.target).hide();
}
}
});
};
Drupal.Collapsiblock.getCookieData = function () {
var cookieString = $.cookie('collapsiblock');
return cookieString ? Drupal.parseJson(cookieString) : {};
};
Run Code Online (Sandbox Code Playgroud)
通过添加以下代码解决了问题:
$('.collapsiblock').not(this).each(function(){
$(this).addClass('collapsiblockCollapsed');
$(this.target).animate({height:'hide', opacity:'hide'}, slidespeed);
});
Run Code Online (Sandbox Code Playgroud)
在以下行之上:
$(this).removeClass('collapsiblockCollapsed');
Run Code Online (Sandbox Code Playgroud)
Kri*_*ard 162
使用not选择器.
例:
$('.collapsiblock').click(function(){
$('.collapsiblock').not(this).each(function(){
$(this).slideUp();
});
$(this).slideDown();
})
Run Code Online (Sandbox Code Playgroud)
试试这个,
例:
$('.collapsiblock').click(function(){
$('.collapsiblock').not(this).slideUp();
$(this).slideDown();
});
Run Code Online (Sandbox Code Playgroud)
这是更好的方法,因为如果你使用每个函数,它将加载,并在将来你有超过数千div所以它将需要很长时间的SlideUp或SlideDown.
| 归档时间: |
|
| 查看次数: |
63270 次 |
| 最近记录: |