3

我正在开发一个基于 OpenSeaDragon 的图片库,我希望能够在收集模式下使用叠加层。基于 OSD 网站 ( http://openseadragon.github.io/ ) 上的各种示例,我设法编写了一个最小的工作示例,但有几个问题我无法解决(请参阅https:// jsfiddle.net/7ox0hg9L/)。

首先,开/关叠加切换工作正常,但如果我平移/缩放图像,叠加会重新出现,即使切换关闭使用parentNode.removeChild().

其次,我似乎无法让叠加工具提示在第一页上始终如一地工作,并且它们从未出现在后续页面上。单选按钮标签上的工具提示在任何页面上都可以正常工作,所以我不确定为什么叠加层上的工具提示不能。

任何建议都会受到欢迎。请记住,我是 javascript 新手。谢谢!

编辑:iangilman 下面的回答和他对 jsfiddle 的编辑让我重回正轨,并帮助我创建了我心目中的画廊。我在这里为可能需要类似功能的人发布了完整的解决方案。谢谢伊恩!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset='utf-8'>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/openseadragon/2.3.1/openseadragon.min.js"></script>
    <style>
        body {
            margin: 0;
            color: #333;
            font-family: Helvetica, Arial, FreeSans, san-serif;
            background-color: #121621;
        }
        .openseadragon{
            width:      800px;
            height:     600px;
            border:     1px solid black;
            color:      #333;
            background-color: black;
        }
        .highlight{
            opacity:    0.4;
            filter:     alpha(opacity=40);
            outline:    6px auto #0A7EbE;
            background-color: white;
        }
        .highlight:hover, .highlight:focus{
            filter:     alpha(opacity=70);
            opacity:    0.7;
            background-color: transparent;
        }
        .nav {
            cursor: pointer;
            display: inline-block;
            font-size: 25px;
        }
        .controls {
            text-align: center; 
            display: table;
            background-color: #eee;
            table-layout: fixed;
            width: 800px;
        }
    </style>
</head>

<body>

<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

<div class="controls">
    <label class="labels"><input id="showOverlays" type="checkbox"><a id="selector" title="">Show overlays</a></label>
    <a class="nav previous" title="Previous" id="prv"> < </a>
    <a class="nav next" title="Next" id="nxt"> > </a>
</div>

<div id="example-runtime-overlay" class="openseadragon" />

<script type="text/javascript">
    var tileSource = {
        Image: {
            xmlns: "http://schemas.microsoft.com/deepzoom/2008",
            Url: "http://openseadragon.github.io/example-images/highsmith/highsmith_files/",
            Format: "jpg",
            Overlap: "2",
            TileSize: "256",
            Size: {
                Height: "9221",
                Width:  "7026"
            }
        }
    };
    var runtimeViewer = OpenSeadragon({
        id:            "example-runtime-overlay",
        prefixUrl:     "openseadragon/images/",
        showSequenceControl: true,
        sequenceMode:  true,
        nextButton:     "nxt",
        previousButton: "prv",
        tileSources: [{
            tileSource: tileSource,
            overlay: [{
                id: 'example-overlay',
                x: 0.43,
                y: 0.47,
                width: 0.15,
                height: 0.20,
                className: 'highlight',
                caption: 'Nice painting'
            }]
        },{
            tileSource: tileSource,
            overlay: [{
                id: 'example-overlay',
                x: 0.65,
                y: 0.05,
                width: 0.12,
                height: 0.12,
                className: 'highlight',
                caption: 'Milton'
            }]
        }]
    });

    var page = 0;
    runtimeViewer.addHandler("page", function (data) {
        page = data.page;
    });

    $('.next').click(function() {
        radio.prop('checked', false);
    });

    $('.previous').click(function() {
        radio.prop('checked', false);
    });

    var radio = $('#showOverlays')
        .prop('checked', false)
        .change(function() {
            if (radio.prop('checked')) {
                var overlay = runtimeViewer.tileSources[page].overlay[0];
                var elt = document.createElement("div");
                elt.id = overlay.id;
                elt.className = overlay.className;
                elt.title = "";
                $(elt).tooltip({
                    content: overlay.caption
                });
                runtimeViewer.addOverlay({
                    element: elt,
                    location: new OpenSeadragon.Rect(overlay.x, overlay.y, overlay.width, overlay.height)
                });
            } else {
                var overlay = runtimeViewer.tileSources[page].overlay[0];
                var element = document.getElementById(overlay.id);
                if (element) {
                    runtimeViewer.removeOverlay(element);
                    delete element;
                }
            }
        });

    $(function() {
        $(document).tooltip();
    });
</script>

</body>

</html>
4

1 回答 1

1

看起来你有一个好的开始!

您正确地添加了叠加层addOverlay,因此您需要使用 删除它们removeOverlay

runtimeViewer.removeOverlay(element);

对于工具提示,不幸的是 OpenSeadragon 的事件处理会干扰 jQuery,因此您必须使用 OpenSeadragon MouseTracker:

function bindTooltip(elt) {
  new OpenSeadragon.MouseTracker({
    element: elt,
    enterHandler: function(event) {
      // Show tooltip
    },
    exitHandler: function(event) {
      // Hide tooltip
    }
  }).setTracking(true);
}
于 2018-03-23T16:47:51.563 回答