我在网页上有许多与放大器互动的按钮。我希望每个人都能根据鼠标事件做三个JavaScript函数之一。
我遇到的问题是,当我点击一个按钮时,其他行会消失但是当我鼠标移出所有行时再次显示。我需要的是:
onmouseover =在悬停时,隐藏非对应元素,焦点保持可见。 (出于各种原因使用opacity = 0。)
onclick =永久隐藏非对应元素,直到点击另一个按钮。
onmouseout =显示所有元素,如果没有点击但是如果点击它只显示焦点元素,直到单击另一个按钮。
您可以在此处查看功能。他们都工作,只是不知道如何得到我需要工作的东西。
function resetall(focus) {
features.eachLayer(function(l) {
var props = l.feature.properties;
props['stroke-opacity'] = 1;
});
features.setGeoJSON(geojson);
};
function clickline(focus) {
features.eachLayer(function(l) {
var props = l.feature.properties;
props['stroke-opacity'] = (props.id !== focus) ? 0.5 : 1;
});
features.setGeoJSON(geojson);
};
function showline(focus) {
features.eachLayer(function(l) {
var props = l.feature.properties;
props['stroke-opacity'] = (props.id === focus) ? 1 : 1;
});
features.setGeoJSON(geojson);
};
function updateheader(focus) {
// Iterate through each feature in the ,
// features object and alter the properties.
features.eachLayer(function(l) {
var props = l.feature.properties;
if (props.id === focus) {
props['stroke-opacity'] = (props.id === focus) ? 1 : 1;
map.setView([props['zlat'], props['zlng']], props['zzoom']);
infoTop.innerHTML = '<div>' + props['header'] + '</div>';
info.innerHTML = '<div>' + props['descript'] + '<br>' + '</div>';
infoImg.innerHTML = '<div>' + props['image'] + '<br>' + '</div>';
} else {
props['stroke-opacity'] = (props.id !== focus) ? 0.0 : 1;
}
});
features.setGeoJSON(geojson);
};
JSfiddle - 在这里演示
希望这是有道理的。 感谢。
埃里克
答案 0 :(得分:0)
类似的问题tohttp://stackoverflow.com/questions/2575420/jquery-binding-event-on-selected-class
使用HTML对象的唯一类名,使用jquery将事件绑定到函数,并将其绑定到具有该类名的所有对象。
答案 1 :(得分:0)
我必须稍微重构你的代码才能使它工作并且更加清晰。它并不完全符合您在请求中所描述的内容,但我会说您应该重新考虑您的期望,例如当你没有点击任何触发器时,只需将鼠标悬停在一起,你应该最终处于与悬停之前相同的状态,但在你的描述中它看起来并不像那样。
其他评论:
updateheader()
弄乱了一点,所以为了清楚起见,我发表了评论。总的来说,代码现在可以运行,但远非完美。您可能希望在清晰度和更高级别的抽象上工作,而不是重复相同的代码部分。祝你好运。
所以这里是http://jsfiddle.net/A7edV/3/:
var button = document.getElementById('trigger');
var map = L.mapbox.map('map', 'ejs06003.ilb3d7on', {
zoomControl: false
}).setView([41.766431, -72.700585], 11);
var geojson = [{
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [
[-72.700585, 41.766431],
[-72.701112, 41.585276]
]
},
properties: {
'id': 0,
'stroke': 'white',
'stroke-opacity': 1,
'stroke-width': 0,
'header': 'reset title',
'descript': 'reset Description',
'quicktext': 'reset quick',
'image': 'reset image',
'link': 'reset link',
'zlat': 41.766431,
'zlng': -72.700585,
'zzoom': 11
}
}, {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [
[-72.676081, 41.766431],
[-72.700585, 41.772385]
]
},
properties: {
'id': 1,
'stroke': '#e74c3c',
'stroke-opacity': 0.5,
'stroke-width': 4,
'header': 'red title',
'descript': 'red Description',
'quicktext': 'red quick',
'image': '<img src="http://selectbylocation.com/i84/img/train.jpg"',
'link': 'red link',
'zlat': 41.772385,
'zlng': -72.700585,
'zzoom': 14
}
}, {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [
[-72.653900, 41.763387],
[-72.636562, 41.772385]
]
},
properties: {
'id': 2,
'stroke': '#3498db',
'stroke-opacity': 0.5,
'stroke-width': 4,
'header': 'blue title',
'descript': 'blue Description',
'quicktext': 'blue quick',
'image': '<img src="http://selectbylocation.com/i84/img/fasttrack.jpg"',
'link': 'blue link',
'zlat': 41.763387,
'zlng': -72.653900,
'zzoom': 14
}
}, {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [
[-72.792525, 41.773561],
[-72.692962, 41.809270],
[-72.621894, 41.810165]
]
},
properties: {
'id': 3,
'stroke': 'green',
'stroke-opacity': 0.5,
'stroke-width': 4,
'header': 'green title',
'descript': 'green Description',
'quicktext': 'green quick',
'image': 'green image',
'link': 'green link',
'zlat': 41.809270,
'zlng': -72.692962,
'zzoom': 12
}
}];
infoTop.innerHTML = '<div>' + 'text' + '</div>';
info.innerHTML = '<div>' + 'text' + '<br>' + '</div>';
infoImg.innerHTML = '<div>' + 'text' + '<br>' + '</div>';
// Create a feature layer that will hold the GeoJSON above.
var features = L.mapbox.featureLayer().addTo(map);
features.setGeoJSON(geojson);
features.on('mouseover', function (e) {
focusline(e.layer.feature.properties.id);
});
reset.onclick = function () {
resetall(0);
// commented out just for clarity
//updateheader(0);
}
var selectedLine = null;
trigger.onclick = function () {
clickline(1, true);
// commented out just for clarity
//updateheader(1);
}
trigger.onmouseover = function () {
clickline(1);
}
trigger.onmouseout = function () {
showline(1);
}
trigger2.onclick = function () {
clickline(2, true);
// commented out just for clarity
//updateheader(2);
}
trigger2.onmouseover = function () {
clickline(2);
}
trigger2.onmouseout = function () {
showline(2);
}
trigger3.onclick = function () {
clickline(3, true);
// commented out just for clarity
//updateheader(3);
}
trigger3.onmouseover = function () {
clickline(3);
}
trigger3.onmouseout = function () {
showline(3);
}
function resetall(focus) {
selectedLine = null;
features.eachLayer(function (l) {
var props = l.feature.properties;
props['stroke-opacity'] = 0.5;
});
features.setGeoJSON(geojson);
};
function clickline(focus, actuallyClicked) {
if (actuallyClicked) {
selectedLine = focus;
}
// Iterate through each feature in the
// features object and alter the properties.
features.eachLayer(function (l) {
var props = l.feature.properties;
props['stroke-opacity'] = (props.id !== focus) ? 0 : 1;
});
// Rerun the geojson
features.setGeoJSON(geojson);
};
function showline(focus) {
if (selectedLine) {
// Iterate through each feature in the
// features object and alter the properties.
features.eachLayer(function (l) {
var props = l.feature.properties;
props['stroke-opacity'] = (props.id === selectedLine) ? 1 : 0;
});
} else {
features.eachLayer(function (l) {
l.feature.properties['stroke-opacity'] = 0.5;
});
}
// Rerun the geojson
features.setGeoJSON(geojson);
};
function updateheader(focus) {
// Iterate through each feature in the ,
// features object and alter the properties.
features.eachLayer(function (l) {
var props = l.feature.properties;
if (props.id === focus) {
props['stroke-opacity'] = (props.id === focus) ? 1 : 1;
map.setView([props['zlat'], props['zlng']], props['zzoom']);
infoTop.innerHTML = '<div>' + props['header'] + '</div>';
info.innerHTML = '<div>' + props['descript'] + '<br>' + '</div>';
infoImg.innerHTML = '<div>' + props['image'] + '<br>' + '</div>';
} else {
props['stroke-opacity'] = (props.id !== focus) ? 0.0 : 1;
}
});
// Rerun the geojson
features.setGeoJSON(geojson);
};