【问题标题】:how can I connect an SVG image to a select menu?如何将 SVG 图像连接到选择菜单?
【发布时间】:2017-05-01 18:20:45
【问题描述】:

好的,我有一个问题,也许是一个简单的问题,我是新手。

我有一个 SVG 图像,它实际上是一张地图(区域地图),分为多个部门(即城市)。没关系,SVG 完美运行。另外,我有一个简单的下拉列表(进入HTML)。

这就是我想要的:

当有人在菜单中选择一个选项(城市)时,选择器(地区)会显示为选中状态。而且,当有人选择选择器(地区)时,菜单(城市)中的选项会显示为选中状态。

我有附加图片。

非常感谢。

更新:

下拉菜单 HTML 代码:

<label for="sel1">Seleziona Area:</label>
<select class="form-control" id="sel1">
<option>1 - Udine Centro</option>
<option>2 - Rizzi / S. Domenico / Cormor / S. Rocco</option>
<option>3 - Laipacco / San Gottardo</option>
<option>4 - Udine sud</option>
<option>5 - Cussignacco</option>
<option>6 - S. Paolo / S. Osvaldo</option>
<option>7 - Chiavris / Paderno</option>
</select>

SVG 图像的 Javascript 代码:

$(document).ready(function() {


    $('g.chiavris').click(function() {
        $('g.chiavris').fadeOut('slow');
    });
    
    $("g.region").hover(function() {

        //mouse over
        $(this).find('.map-image').css('fill', '#8B8B8B');
    	$(this).find('.map-title').css('display', 'block');
    }, function() {

        //mouse out
        $(this).find('.map-image').css('fill', '#ccc');
        $(this).find('.map-title').css('display', 'none');

    });

	$('.region').click(function(event) {

		var regions = $('.region');
		console.log(regions);

		for(var i=0; i<regions.length; i++){
			console.log('tutti messi normali '+ i);
			$(regions[i]).find('.map-image').css('fill', '#ccc');
        	$(regions[i]).find('.map-title').css('display', 'none');
			$(regions[i]).bind('mouseenter mouseleave');

		}

		//DOPO
        $(this).find('.map-image').css('fill', '#FF7409');
        $(this).find('.map-title').css('display', 'block');
		
        $(this).unbind('mouseenter mouseleave');
        
    });


});

更新 2:

好的,谢谢大家,我已经升级了你的代码:

// per selezionare i "polygon" che influisce sul select
    $(".map-image").on('click', function(evt) {
        // Get the id of the region we clicked on
        var regionId = evt.target.id;
        // Update the dropdown
        $('#sel1 option').removeAttr('selected')
            .filter('[value=' + regionId + ']')
            .attr('selected', true);
        // Highlight the relevant region
        setRegion(regionId);
    });

    // Per selezionare dal select e avere il colore nella mappa
    $("#sel1").change(function(evt){
      //console.log($(this).val());
      var name_region = ($(this).val());
      var regions = $(document).find('#'+ name_region).get(0);
      //console.log(regions);
      $(regions).css('fill', '#FF7409');
    });

  

    /*function onRectClick(evt)
    {
      // Get the id of the region we clicked on
      var regionId = evt.target.id;
      // Update the dropdown
      $("#sel1").val(regionId).change();
      // Highlight the relevant region
      setRegion(regionId);
    }*/

    function onSelectChange() {
        // Get selected class from dropdown
        var selectedRegion = $("#sel1").val();
        // Highlight the relevant region
        setRegion(selectedRegion);
    }

    function setRegion(regionId) {
        // Remove "selected" class from current region
        $("polygon.selected").removeClass("selected");
        // Add "selected" class to new region
        $('polygon#' + regionId).addClass("selected");

        // Note: for addClass() etc to work on SVGs, you need jQuery 3+
    }


    // Init map based on default select value
    onSelectChange();

【问题讨论】:

  • 到目前为止你尝试过什么?你能显示一些代码吗?
  • 我更新了我的帖子。您可以检查代码。我无法附上 SVG 代码,因为它对于帖子来说太长了 :( 如果你需要它,请告诉我如何附上它。谢谢。如果你需要更多信息问我,谢谢!!

标签: javascript jquery html svg


【解决方案1】:

你的方法比它需要的更复杂。悬停的东西你可以留给 CSS。

点击和选择的变化只能通过事件处理程序来处理。

您需要做的就是给每个地图区域一个id 并为每个&lt;option&gt; 元素分配一个相应的值。

然后只更新select,根据你是点击还是改变select字段来改变区域的class。

$("rect").click(onRectClick);
$("#sel1").change(onSelectChange);


function onRectClick(evt)
{
  // Get the id of the region we clicked on
  var regionId = evt.target.id;
  // Update the dropdown
  $("#sel1").val(regionId);
  // Highlight the relevant region
  setRegion(regionId);
}

function onSelectChange()
{
  // Get selected class from dropdown
  var selectedRegion = $("#sel1").val();
  // Highlight the relevant region
  setRegion(selectedRegion);
}

function setRegion(regionId)
{
  // Remove "selected" class from current region
  $("rect.selected").removeClass("selected");
  // Add "selected" class to new region
  $('rect#'+regionId).addClass("selected");
  
  // Note: for addClass() etc to work on SVGs, you need jQuery 3+
}


// Init map based on default select value
onSelectChange();
rect {
  fill: #ccc;
  stroke: #999;
  stroke-width: 2;
  cursor: pointer;
}

rect:hover {
  fill: #888;
}

rect.selected {
  fill: #ff7409;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  
<svg width="300" height="300">
  <rect id="region1" x="50" y="0" width="100" height="100"/>
  <rect id="region2" x="150" y="0" width="100" height="100"/>
  <rect id="region3" x="0" y="100" width="100" height="100"/>
  <rect id="region4" x="100" y="100" width="100" height="100"/>
  <rect id="region5" x="200" y="100" width="100" height="100"/>
  <rect id="region6" x="50" y="200" width="100" height="100"/>
  <rect id="region7" x="150" y="200" width="100" height="100"/>
</svg>  

<div>

  <label for="sel1">Seleziona Area:</label>
  <select class="form-control" id="sel1">
    <option value="region1">1 - Udine Centro</option>
    <option value="region2">2 - Rizzi / S. Domenico / Cormor / S. Rocco</option> 
    <option value="region3">3 - Laipacco / San Gottardo</option>
    <option value="region4">4 - Udine sud</option>
    <option value="region5">5 - Cussignacco</option>
    <option value="region6">6 - S. Paolo / S. Osvaldo</option>
    <option value="region7">7 - Chiavris / Paderno</option>
  </select>

</div>

【讨论】:

  • 我已经发布了一个更新,如果你愿意,请检查它,谢谢。
  • 什么更新?自从我发布此答案以来,您的问题没有改变。
  • 我已经更新了你的代码。一切都完美无缺。只有一件事......当我在菜单上选择城市时,该地区仍然被选中(橙色),没关系,但是当我用鼠标“悬停”时,选择消失(所以橙色消失了)
  • 非常感谢大家
  • @PaulLeBeau 你能给我发消息吗?我正在尝试用地图做一些非常相似的事情。地图充满了 组,我在无法识别的表达式的行上遇到错误: rect# $('rect#'+regionId).addClass("selected");
猜你喜欢
  • 1970-01-01
  • 2015-11-15
  • 1970-01-01
  • 2013-09-27
  • 1970-01-01
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多