javascript - 如何分享弹窗的url?

标签 javascript jquery html xml

我找到了一个将 xml 数据加载到弹出框的代码。我尝试分享弹出框的 URL。但代码不支持分享URL。当尝试在新选项卡中打开弹出窗口的 URL 时,需要直接显示弹出窗口。我怎样才能实现代码?

这是我找到的代码。

      $(document).ready(function(){
    $.ajax({
        type:"GET",
        url:"data.xml",
        dataType:"xml",
        success:xmlParser
    });
});

function xmlParser(xml){
    xml = $(xml).children();
    let total = $(xml).children().length;
    $(xml).children().each(function (idx) {     
        
        let tag = $(this).prop("tagName");
        let nextIdx = idx + 1;
        let prevIdx = idx - 1;
        //to make cyclic
        nextIdx = nextIdx == total ? 0 : nextIdx;
        prevIdx = prevIdx == -1 ? (total -1) : prevIdx;

        let image = '<img style="background-image:url(' + $(this).find("image").text() + ')"' + '" />';
        let image2 = '<div><img src="' + $(this).find("image").text() + '" width="100%" alt="' + '" />' + '</div>';
        let head = '<div>' + $(this).find("head").text() + '</div>';
        
        
        let html = `<div class="col-sm-4 random" id="random">
                    <div class="thumbnail randomdiv3" id="border" >
                    <a href="#${tag + idx}" id="openModalBtn">
                            <div>${image}</div>
                            <h5>${head}</h5>
                        </a>
                    </div>
                 </div>`;
        let popup = `<div id="${tag + idx}" class="overlay">
                <div class="popup">
                
                <a href="#${tag + prevIdx}" class="previous round">&#8249;</a>
                <a href="#${tag + nextIdx}" class="next round">&#8250;</a>
                    <h6>${head}</h6>
                    <a class="close" href="#">&times;</a>
                    <div>${image2}</div>
                </div>
            </div>`;


        $("#xmldata").append(html);
        $("#popup").append(popup);
    });
}
.overlay {
              position: fixed;
              top: 0;
              bottom: 0;
              left: 0;
              right: 0;
              background: rgba(0, 0, 0, 0.7);
              transition: opacity 500ms;
              visibility: hidden;
              opacity: 0;
            }
            .overlay:target {
              visibility: visible;
              opacity: 1;
            }
            .popup {
              margin: 70px auto;
              padding: 20px;
              background: #fff;
              border-radius: 5px;
              width: 30%;
              position: relative;
              transition: all 2s ease-in-out;
            }
            .popup .close {
              position: absolute;
              top: 20px;
              right: 30px;
              transition: all 200ms;
              font-size: 30px;
              font-weight: bold;
              text-decoration: none;
              color: #333;
            }
            .popup .content {
              max-height: 30%;
              overflow: auto;
            }
            .thumbnail img {
            width: 100%;
            height: 272px;
            background-size: cover;
            background-repeat: no-repeat;
            background-position: 50% 20%;
            vertical-align: middle;
            position: inherit;
            }

            .previous {
            background-color: #000;
            color: white;
            padding: 8px 16px;
            }

            .next {
                background-color: #000;
                color: white;
                padding: 8px 16px;
            }

            .round {
                border-radius: 50%;
            }
<div class="row" id="xmldata"></div>

<div id="popup"></div>

http://next.plnkr.co/edit/70HUWx64QlKhfMIh?open=lib%2Fscript.js

最佳答案

您可以使用 url hash 来共享弹出 url

我已经更新了您的code请检查一下并告诉我,每次您单击链接打开弹出窗口时,它都会更新您的窗口哈希,您可以将该网址分享给任何将直接打开图像弹出窗口的人

关于javascript - 如何分享弹窗的url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52399242/

相关文章:

javascript - 从外部 html 文件加载内容

javascript - 能够翻转 div 框、改变它们的大小以及与内容交互

javascript - 在 jquery 中悬停时在图像上显示链接

Javascript Switch Case 在社交引擎中不起作用

javascript - 如何在VueJS中动态显示图像?

javascript - Chrome 扩展中的 TreeWalker

javascript - 滚动部分时是否有用于自定义转换的整页 js 代码?

javascript - Bootstrap 导航栏不会关闭

javascript - 发送一个变量到jquery

javascript - Web 编译器使用 require() 将 CoffeeScript 转换为 JavaScript