【问题标题】:popup image with no html code only css and javascript没有html代码的弹出图像只有css和javascript
【发布时间】:2021-03-08 16:31:40
【问题描述】:

所以我有一个使用 pannellum 的 360 度虚拟游览,我在 css 中设计了这个热点代码

.custom-hotspot {
    height: 30px;
    width: 30px;
    border-radius: 50%;
    background: rgb(253, 253, 255);
}

我希望当你点击热点弹出一个图像但不使用html标签时只使用css和javascript,我尝试使用这个

.custom-hotspot:hover {
  content: url(https://i.ibb.co/5c9zFfq/DCIM-100-MEDIA-DJI-0293-JPG.jpg); /* no need for qoutes */
  width: 100px;
  height: 100px;
  border-radius: 20%;
}

但这不是弹出式图像,也不一样。 有谁知道这个的解决方案吗?

【问题讨论】:

    标签: javascript html css popup pannellum


    【解决方案1】:

    如果弹出窗口是指单独的浏览器窗口,则不是..

    但是,如果您想在元素悬停时在光标旁边显示图像,您可以将其显示为伪元素中的背景。

    .custom-hotspot:hover::after {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      width: 100px;
      height: 100px;
      border-radius: 20%;
      background-image: url(https://i.ibb.co/5c9zFfq/DCIM-100-MEDIA-DJI-0293-JPG.jpg);
    }
    

    确保父元素具有定义的位置属性,例如position: relativeabsolute。否则,图像将显示在最近定义它的祖父母的顶部。

    编辑..

    .clicked::after {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      width: 100px;
      height: 100px;
      border-radius: 20%;
      background-image: url(https://i.ibb.co/5c9zFfq/DCIM-100-MEDIA-DJI-0293-JPG.jpg);
    }
    

    Javascript:

    // this gets HTML collection of all elements with the 'custom-hotspot' class
    const elementCollection = document.getElementsByClassName('custom-hotspot');
    
    // add a click listener to each element
    elementCollection.forEach(e => {
       e.addEventListener('click', handleClick, {passive: true});
    });
    
    /// here we toggle our 'clicked' class
    function handleClick(event) {
       // if '.target' does not work as expected try '.currentTarget'
       event.target.classList.toggle('clicked');
    }
    

    【讨论】:

    • 那行得通!但现在的问题是图像如何保持静态,就像当你点击它时它会出现,当你再次点击它时它会消失
    • 尝试使用伪选择器:focus而不是:hover
    • 对.. 仅适用于输入.. 而不是:hover,写一个像.clicked 这样的类。然后,您需要使用 javascript 将 .clicked 类添加到单击的元素。
    • 你能告诉我怎么做吗?我是新手
    • 别担心,乐于助人! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2021-09-23
    • 2010-09-25
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多