【问题标题】:Hover over a building floor in HTML/CSS将鼠标悬停在 HTML/CSS 中的建筑楼层
【发布时间】:2021-05-26 20:56:57
【问题描述】:

我试图在悬停时更改地板的颜色。 https://imgur.com/a/pXEmNXp 这是我正在尝试做的图像。

我在这篇文章中尝试了答案:How can we draw 3D bulding structure from existing image Jquery,Css?,但是当我实际悬停在地板上时它不起作用,它只在我悬停在图像底部的随机位置时显示。

我该怎么做才能在我悬停时显示特定楼层,以及如何实际实现每个楼层的地图属性?

如果有帮助,这是我的代码:

$(document).ready(function () {

        $('map').on('mouseenter', function () {
            $('.highlight-4').show();
        })

        $('map').on('mouseleave', function () {
            $('.highlight-4').hide();
        })
    });
.highlight {
     z-index: 9999;
     display: none;
     pointer-events: none;
  }

  .building {
     position: absolute;
     margin-left: 11%;
     box-shadow: 0 15px 30px rgba(0, 0, 0, .6);;
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
HTML

        <img class="highlight highlight-4" src="/static/floor-4.png" alt="">
        <img class="highlight highlight-3" src="/static/floor-3.png" alt="">
        <img class="highlight highlight-2" src="/static/floor-2.png" alt="">
        <img class="highlight highlight-1" src="/static/floor-1.png" alt="">
        <img class="highlight highlight-0" src="/static/floor-0.png" alt="">
        <img usemap="#image-map" class="building" src="/static/apartments.jpeg" alt="">
        <map name="image-map">
            <area target="" alt="" title="" href="" coords="1121,326,197,662,191,747,1130,473,1398,664,1387,544"
                shape="poly">
        </map>

【问题讨论】:

    标签: html jquery css


    【解决方案1】:

    最好的方法似乎是将您的建筑图像实现为带有 &lt;polyline&gt; 叠加层的 SVG 文件。通过这种方式,您可以使用 CSS 选择器并对布局进行更多控制。您可以在 SVG、javascript 等内部使用 &lt;a&gt;

    我获取了您的图像并将其加载到 Draw SVG 以创建所需的顶层 &lt;polyline&gt;。在本地保存 DrawSVG 绘图后,我只需将 &lt;polyline&gt; 复制到我自己的 SVG 文件中,正如您所见,该文件无需任何进一步的 HTML 即可工作,并添加了一些 CSS 来突出显示 :hover

    您可以将此 SVG 插入到您的 html 中,就好像它是 &lt;img&gt;,它只是另一个标签(有好处)。

    更新

    重新访问了我发布的原始代码并将其替换为全新的代码。该代码现在是一个 HTML 页面,它使用带有 OP 图像的 SVG 文件,并展示了如何在 :hover 上使用带有一些演示文本的 &lt;a&gt;(又是顶楼)。

    HTML 页面也通过在 CSS calc()@media 查询中使用数学来实现“响应式”(转到“整页”并调整浏览器大小,还可以使用浏览器在各种设备上检查页面开发者工具,“纵向”和“横向”模式)。

    数学的解释可以在我给出的其他答案中找到:

    现在您创建其他楼层....

    /*
        Visit below answers on Stackoverflow for an explanation
        of the math used for responsive sizing of elements.
    
        https://stackoverflow.com/a/62033317/2015909
        https://stackoverflow.com/a/61867702/2015909
    */
    /**************************/
    /* preferred global rules */
    /**************************/
    html,body               { box-sizing: border-box; width: 100%; max-width: 100%; height: 100% }
    *::before,*::after, *   { box-sizing: inherit }
    body                    { margin: 0 }
    
    /* responsive base font size using y = mx + b */
    html    { font-size: calc(0.625vmin + 0.75rem) } /* (320,14)(1280,20) */
    body    { font-size: 1rem }
    
    [band]  { display: flex; flex-flow: column wrap; align-content: center }
    
    [padded="1"], [padded="0"] [band*="padded"] {
    /*
        responsive page padding
        and responsive band padding (same as responsive page padding, but at band level)
        p1(320,32) p2(1920, 72) => 0.025x + 24
        p3(320, 8) p4(1920,320) => 0.195x - 54.4 
    
        'Band padding' is only active when 'page padding' is off 
    */
        padding: calc( 2.5vh + 24px) calc(19.5vw - 54.4px);
    }
    
    /* Making sure the image fits in any viewport, either portrait or landscape */
    @media (orientation: portrait ) { #construction-site { height: auto; width: 100% } }
    @media (orientation: landscape) { #construction-site { height: 100%; width: auto } }
    <body padded="0">
    <div id="construction-site" band="padded">
        <svg viewbox="0 0 1600 1200"
             xmlns="http://www.w3.org/2000/svg"
             xmlns:xlink="http://www.w3.org/1999/xlink">
    
            <style>
                .line       { stroke: Black; stroke-opacity: 0; stroke-width: 2px; cursor: pointer } 
                .polyline   { fill  : Red  ; fill-opacity  : 0 }
        
                .floor-text { fill: Black; stroke: none; fill-opacity: 0;
                              cursor: default; pointer-events: none;
                              font-weight: bold; font-family: sans-serif;
                              /* responsive font using y = mx + b */
                              font-size: calc(-1vmin + 51.2px); /* (320,48)(1920,32) */
                            }
    
                .floor:hover>.line       { fill-opacity: .4; stroke-opacity: .8 }
                .floor:hover>.floor-text { fill-opacity: .7 }
            </style>
    
            <image href="https://i.imgur.com/0i4N0d3.jpg"/>
    
            <a id="top-floor" class="floor" rel="noopener" target="_blank" href="javascript:void(0)" title="top floor, click for details...">
                <polyline class="line polyline" 
                    points="201.242,678.473 1121.43, 333.16 1370.24,
                            553.473 1387.74, 668.473 1127.49,
                            474.098 189.242, 753.473 202.242, 678.973"/>
    
                <line class="line" x1="1121.42" y1="333.472" x2="1127.45" y2="474.097"/>
                <text class="floor-text" x="800" y="150" text-anchor="middle">top floor demo text</text>
            </a>
        </svg>
    </div>
    </body>

    【讨论】:

    • 我会尝试实施其他楼层,但你说得对,它有效。谢谢你:)
    • 我确实有一个问题,我以前没有画过svg,我用什么形状来画出确切的地板?矩形形状似乎效果不佳
    • 所以回答我自己的问题我想通了,如果你想自己做,只需从元素折线中选择,然后绘制你的形状,然后你可以在另一个选项卡中打开图像并检查它,复制折线,一切就绪。非常感谢Rene的回答:)
    • 对于不规则形状,您可以使用&lt;polyline&gt;&lt;path&gt;MDN: Basic shapes。但我建议您在 DrawSVGInkScape 之类的应用程序中做一些基本的事情,保存 SVG 文件并在编辑器中打开它以查看其工作原理。有点学习曲线...
    • 完全不相关,但这是我不久前在 Codepen 上制作的 2 个演示:Toggle SVG design grid with SVG on topToggle SVG design grid as body background。只是给你一个想法.....
    猜你喜欢
    • 1970-01-01
    • 2015-12-20
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多