简体   繁体   English

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

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

I have a number of buttons on a webpage that interact with a amp. 我在网页上有许多与放大器交互的按钮。 I want each to do one of three JavaScript functions based on the mouse event. 我希望每一个都基于鼠标事件执行三个JavaScript函数之一。

The problem I am having is that when I click on a button the other lines disappear but when I mouseout all lines show again. 我遇到的问题是,当我单击按钮时,其他行消失了,但是当我将所有行都移出鼠标时,它们又再次显示了。 What I need is: 我需要的是:

onmouseover = While hovering, hide non-corresponding elements, focus to stay visible. onmouseover =悬停时,隐藏不对应的元素,将焦点保持可见。 (Using opacity = 0 for various reasons.) (出于各种原因,使用不透明度=0。)

onclick = Permanently hide non-corresponding elements until another button is clicked. onclick =永久隐藏不对应的元素,直到单击另一个按钮。

onmouseout = Show all elements if it wasn't clicked but if it was clicked only show focused element until another button is clicked. onmouseout =如果未单击,则显示所有元素,但如果单击,则仅显示焦点元素,直到单击另一个按钮。

You can see the functions here. 您可以在此处查看功能。 They all work, just not sure how to get what I need to work. 他们都工作,只是不确定如何获得我需要的工作。

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 - Demo here JSfiddle-演示在这里

Hopefully this makes sense. 希望这是有道理的。 Thanks. 谢谢。

Eric 埃里克

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

Use a unique class name for the HTML objects to have and bind the event to your functions using jquery to all objects with that class name. 为HTML对象使用唯一的类名,并使用jquery将事件绑定到具有该类名的所有对象的函数。

I had to refactor your code a bit to make it work and be more clear. 我不得不对您的代码进行一些重构,以使其工作并变得更加清晰。 It does not do exactly what you described in your request, but I'd say you should rethink your expectations, because eg when you don't click any trigger, just hover over and out, you should end up with the same state as before hovering, but in your description it doesn't look like that. 它不完全符合您在请求中描述的内容,但是我想说您应该重新考虑您的期望,因为例如,当您不单击任何触发器时,只需将鼠标悬停一遍,您应该以与以前相同的状态结束徘徊,但在您的描述中却并非如此。

Other remarks: 其他说明:

  1. First of all you cannot use the same function for onmouseover and click event (unless you pass different parameters to them), because you want different things to happen on those events. 首先,您不能对onmouseover和click事件使用相同的函数(除非您将不同的参数传递给它们),因为您希望这些事件发生不同的事情。
  2. From your description it seems like you would need 3 values of opacity to make lines invisible, semi-visible and visible. 从您的描述看来,您需要3个不透明度值才能使线条不可见,半可见和可见。 I also asumed that by default all the lines are invisible. 我还假设默认情况下所有行都是不可见的。
  3. I fixed your reset button, too. 我也修复了您的重置按钮。
  4. updateheader() messes up a bit, so I commented it out for clarity. updateheader()有点混乱,因此为了清楚起见,将其注释掉。

Overall, the code works now, but is far from perfect. 总体而言,该代码现在可以运行,但还远远不够完美。 You may want to work on clarity and higher level of abstraction instead of repeating same parts of code. 您可能需要工作在清晰度和更高的抽象水平上,而不是重复代码的相同部分。 Good luck. 祝好运。

So here it is http://jsfiddle.net/A7edV/3/ : 所以这里是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