【问题标题】:onmouseover, onmouseout and onclick logic to show/hideonmouseover、onmouseout 和 onclick 逻辑来显示/隐藏
【发布时间】:2014-07-16 18:48:19
【问题描述】:

我在网页上有许多与放大器交互的按钮。我希望每个人都根据鼠标事件执行三个 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 - 演示在这里

希望这是有道理的。 谢谢。

埃里克

【问题讨论】:

  • 在您的描述中,您没有涵盖所有边缘情况。当您单击trigger1 然后将鼠标悬停在另一个按钮上时,您期望会发生什么? trigger1 显示的线条是否应该保持可见(我说应该)还是你想在那一刻显示两条线?
  • 你是对的。我错过了。如果您单击任何按钮(除了重置 - 仍然必须修复)然后将鼠标悬停在另一个按钮上,它应该显示但不透明度较低,只是为了显示位置。然后单击该按钮时,它应该以完全不透明的方式显示该行并隐藏其余部分。

标签: javascript jquery html logic mapbox


【解决方案1】:

我不得不稍微重构您的代码以使其工作并更清晰。它并没有完全按照您在请求中描述的那样做,但我想说您应该重新考虑您的期望,因为例如当您不点击任何触发器时,只需将鼠标悬停在上方,您最终应该会得到与悬停之前相同的状态,但在您的描述中,它看起来不像那样。

其他说明:

  1. 首先,您不能对 onmouseover 和 click 事件使用相同的函数(除非您向它们传递不同的参数),因为您希望在这些事件上发生不同的事情。
  2. 根据您的描述,您似乎需要 3 个不透明度值才能使线条不可见、半可见和可见。我还假设默认情况下所有线条都是不可见的。
  3. 我也修复了你的重置按钮。
  4. 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);
};

【讨论】:

  • 你完成了很多我遇到的问题。我非常感谢您的帮助、cmets 和提示。极好的工作。非常感谢。我希望这对其他人有所帮助。
【解决方案2】:

类似问题http://*.com/questions/2575420/jquery-binding-event-on-selected-class

为 HTML 对象使用唯一的类名,并使用 jquery 将事件绑定到您的函数到具有该类名的所有对象。

【讨论】:

  • 这是最好的方法吗?我会认为它更简单。