简体   繁体   English

在事件上移动 openlayers 缩放工具

[英]Move openlayers zoom tools on event

Trying to reproduce something similar to Openlayers with sidebar where once the sidebar is expanded the .ol-zoom margin-left is modified to change its position but I can't use jquery in my project, so looks for Vanilla JS or Angular based solution.尝试使用侧边栏重现类似于Openlayers 的内容,一旦侧边栏展开, .ol-zoom margin-left被修改以更改其位置,但我无法在我的项目中使用 jquery,因此寻找基于 Vanilla JS 或 Angular 的解决方案。

I saw that its quiet easy to change position of the openlayers Zoom buttons as answered here but I would like to change the position on some event trigger like a (sidebar-toggle button) button click.我看到它很容易改变 openlayers 缩放按钮的位置,正如这里所回答的那样但我想改变一些事件触发器的位置,比如(侧边栏切换按钮)按钮点击。

Thanks谢谢

In JS you can dynamically add css to the application, for example例如,在 JS 中,您可以将 css 动态添加到应用程序中

    var css =
        '.menuSeparator {' +
        '    border-bottom: solid 1px black;' +
        '}';

    var head = document.head || document.getElementsByTagName('head')[0];
    var style = document.createElement('style');
    style.type = 'text/css';
    if (style.styleSheet){
        style.styleSheet.cssText = css;
    } else {
        style.appendChild(document.createTextNode(css));
    }
    head.appendChild(style);

Here is my solution这是我的解决方案

const olZoom = document.getElementsByClassName("ol-zoom");
const sideBarElements = document.getElementsByClassName("sidebar-left");
if(!this.sidebarVisibility) {
  (olZoom[0] as HTMLElement).style.marginLeft = "0px";
  (olZoom[0] as HTMLElement).style.marginTop = "60px";
} else {
  setTimeout(() => {
    (olZoom[0] as HTMLElement).style.marginLeft = ((sideBarElements[0] as HTMLElement).offsetWidth - 10) + 'px';
    (olZoom[0] as HTMLElement).style.marginTop = "0px";
  }, 50);
}

major issue what I was facing was that I wasn't casting Element to HTMLElement in typescript as explained here which was blocking me to apply my margin style我面临的主要问题是我没有在打字稿中将ElementHTMLElement ,正如这里所解释的那样这阻止了我应用我的margin样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM