收集模式中的OpenSeaDragon叠加和工具提示问题

时间:2018-03-20 22:55:04

标签: javascript css openseadragon

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

首先,开/关叠加切换效果正常,但如果我平移/缩放图像,叠加会重新出现,即使切换关闭也会使用parentNode.removeChild()从DOM中删除元素。

其次,我似乎无法使叠加工具提示在第一页上始终如一地工作,并且它们永远不会出现在以下页面上。 radiobutton标签上的工具提示在任何页面上都能正常工作,所以我不确定为什么叠加上的工具提示不会。

欢迎提出任何建议。请记住,我是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>

1 个答案:

答案 0 :(得分:0)

看起来你有个好的开始!

您正在使用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);
}
相关问题