繁体   English   中英

onmouseover,onmouseout和onclick逻辑以显示/隐藏

[英]onmouseover, onmouseout and onclick logic to show/hide

我在网页上有许多与放大器交互的按钮。 我希望每一个都基于鼠标事件执行三个JavaScript函数之一。

我遇到的问题是,当我单击按钮时,其他行消失了,但是当我将所有行都移出鼠标时,它们又再次显示了。 我需要的是:

onmouseover =悬停时,隐藏不对应的元素,将焦点保持可见。 (出于各种原因,使用不透明度=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-演示在这里

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

埃里克

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

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

我不得不对您的代码进行一些重构,以使其工作并变得更加清晰。 它不完全符合您在请求中描述的内容,但是我想说您应该重新考虑您的期望,因为例如,当您不单击任何触发器时,只需将鼠标悬停一遍,您应该以与以前相同的状态结束徘徊,但在您的描述中却并非如此。

其他说明:

  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);
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM