我正在建立台湾人口普查数据的在线交互式地图,我有一个JavaScript问题。Javascript和JQUERY:使用点击功能打开和关闭div
地图占用了页面的全部宽度和高度,但是当单击两个div(click_1956和click_1966)时,页面右侧会打开两个容器,并且地图的宽度会相应缩小。
在这些容器中显示图表。每个容器都有一个容纳图形的div。
一切正常,但如果我单击click_1956并打开chart_container_1956,然后单击click_1966并打开chart_container_1966,当再次单击click_1966关闭chart_container_1966时,则chart_container_1956仍处于打开状态。我希望在关闭任何容器时关闭所有容器。
我希望这是有道理的。我粘贴下面的代码。如果没有意义,我很抱歉。这是我第一次使用这个网站。谢谢!
CSS
#charts_container_1956 {
position:absolute;
bottom:0;
width:0;
height:100%;
z-index:10;
background:#FAFAFF;
display:none;
}
#chart_1956 {
position:absolute;
bottom:0px;
top:0;
width:78%;
height:100%;
float:left;
left:21%;
z-index:1000;
background:#B6CEFF;
display: none;
}
#click_1956{
width:25px;
height:25px;
opacity:0.85;
background-image:url("images/chart.png");
background-size:100%;
background-repeat:no-repeat;
background-position:center;
cursor: pointer;
margin-left:10px;
margin-bottom:6px;
}
#click_1956:hover {
background-color: #B6CEFF;
}
#charts_container_1966 {
position:absolute;
bottom:0;
width:0;
height:100%;
z-index:10;
background:#FAFAFF;
display:none;
}
#chart_1966 {
position:absolute;
bottom:0px;
top:0;
width:78%;
height:100%;
float:left;
left:21%;
z-index:1000;
background:#B6CEFF;
display: none;
}
#click_1966{
width:25px;
height:25px;
opacity:0.85;
background-image:url("images/chart.png");
background-size:100%;
background-repeat:no-repeat;
background-position:center;
cursor: pointer;
margin-left:10px;
margin-bottom:6px;
}
#click_1966:hover {
background-color: #B6CEFF;
}
的Javascript
$(document).ready(function(){
$('#click_1956').click(function() {
var clicks = $(this).data('clicks');
if (clicks) {
$("#map").animate({
width: "100%"
}, 300);
$('#charts_container_1956').animate({
width: "0"
},300);
$("#chart_1956").fadeToggle(100);
} else {
$("#map").animate({
width: "20.5%"
}, 300);
$('#charts_container_1956').animate({
width: "79.5%"
},300);
$("#chart_1956").fadeToggle(1000)
}
$(this).data("clicks", !clicks);
});
});
$(function() {
var categories = ['0-4', '5-9', '10-14', '15-19',
'20-24', '25-29', '30-34', '35-39', '40-44',
'45-49', '50-54', '55-59', '60-64', '65-69',
'70-74', '75-79', '80-84', '85-89', '90-94',
'95-99', '100 + '];
$(document).ready(function() {
$('#chart_container_1956').highcharts({
chart: {
type: 'bar'
},
title: {
text: '人口金字塔'
},
subtitle: {
text: '1956年'
},
credits: {
enabled: false
},
exporting: {
enabled: false
},
xAxis: [{
categories: categories,
reversed: false,
labels: {
step: 1
}
}, { // mirror axis on right side
opposite: true,
reversed: false,
categories: categories,
linkedTo: 0,
labels: {
step: 1
}
}],
yAxis: {
title: {
text: null
},
labels: {
formatter: function() {
return (Math.abs(this.value)/1000000) + 'M';
}
},
min: -2000000,
max: 2000000
},
plotOptions: {
series: {
stacking: 'normal'
}
},
tooltip: {
formatter: function() {
return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br>' +
'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y), 0);
}
},
series: [{
name: '男',
data: [-930097, -708733, -478299, -487623, -336419, -362280, -325520, -281719, -251974, -205500, -153693, -109738, -71686, -47206, -45708, -0, -0, -0, -0, -0, -0]
}, {
name: '女',
data: [886484, 670598, 448470, 463230, 403966, 356909, 297371, 240970, 203159, 173283, 133860, 105011, 74621, 57448, 78806, 0, 0, 0, 0, 0, 0]
}]
});
});
});
$(document).ready(function(){
$('#click_1966').click(function() {
var clicks = $(this).data('clicks');
if (clicks) {
$("#map").animate({
width: "100%"
}, 300);
$('#charts_container_1966').animate({
width: "0"
},300);
$("#chart_1966").fadeToggle(100);
} else {
$("#map").animate({
width: "20.5%"
}, 300);
$('#charts_container_1966').animate({
width: "79.5%"
},300);
$("#chart_1966").fadeToggle(1000)
}
$(this).data("clicks", !clicks);
});
});
$(function() {
var categories = ['0-4', '5-9', '10-14', '15-19',
'20-24', '25-29', '30-34', '35-39', '40-44',
'45-49', '50-54', '55-59', '60-64', '65-69',
'70-74', '75-79', '80-84', '85-89', '90-94',
'95-99', '100 + '];
$(document).ready(function() {
$('#chart_container_1966').highcharts({
chart: {
type: 'bar'
},
title: {
text: '人口金字塔'
},
subtitle: {
text: '1966年'
},
credits: {
enabled: false
},
exporting: {
enabled: false
},
xAxis: [{
categories: categories,
reversed: false,
labels: {
step: 1
}
}, { // mirror axis on right side
opposite: true,
reversed: false,
categories: categories,
linkedTo: 0,
labels: {
step: 1
}
}],
yAxis: {
title: {
text: null
},
labels: {
formatter: function() {
return (Math.abs(this.value)/1000000) + 'M';
}
},
min: -2000000,
max: 2000000
},
plotOptions: {
series: {
stacking: 'normal'
}
},
tooltip: {
formatter: function() {
return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br>' +
'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y), 0);
}
},
series: [{
name: '男',
data: [-1018498, -996308, -919856, -682964, -324702, -461254, -430342, -436479, -377743, -306094, -261970, -185797, -127501, -79309, -43250, -20839, -11100, -0, -0, -0, -0]
}, {
name: '女',
data: [959981, 943937, 872920, 671050, 446428, 458478, 399311, 354333, 293547, 234241, 195507, 161451, 119448, 86079, 54002, 32911, 25133, 0, 0, 0, 0]
}]
});
});
});
你的第一codebox(开始用CSS)似乎含有其他两个盒子的内容。 你还可以添加HTML吗? 您可能想向我们展示您目前使用Runnable Code Snippets的内容:http://blog.stackoverflow。com/2014/09/introduction-runnable-javascript-css-and-html-code-snippets/ – BillyNate
我在哪里可以编辑我的问题,以包含与我的代码的Runnable代码片段的链接? – leviwest
尝试https://jsfiddle.net/,在那里你可以发布你的代码并分享链接 – Jayababu