blazor服务器端的可调整大小和可拖动自定义对话框

时间:2020-07-03 06:33:03

标签: javascript .net-core modal-dialog blazor blazor-server-side

我正在研究Core 3.1中的Blazor组件。我只需要使用javscript(无需jquery-ui)在blazor中创建自定义可拖动对话框。有人可以帮助我实现这一目标。我尝试了一些小提琴,但其中的控件不起作用。

1 个答案:

答案 0 :(得分:1)

这只是一个示例,您可以根据自己的要求进行修改。
您可以在site.css或样式标签中编写此CSS

<style>
            #mydiv {
                position: absolute;
                z-index: 9;
                background-color: #f1f1f1;
                text-align: center;
                border: 1px solid #d3d3d3;
            }
    
            #mydivheader {
                padding: 10px;
                cursor: move;
                z-index: 10;
                background-color: #2196F3;
                color: #fff;
            }
        </style>

在您的烈性子元素中

@page "/"
    
    @inject IJSRuntime JsRuntime;
    
        <h1>Draggable DIV Element</h1>
    
        <div id="mydiv">
            <div id="mydivheader">Header</div>
            <p>This is only one sample</p>
        </div>
    
    
    
        @code {
    
    
            protected override Task OnAfterRenderAsync(bool firstRender)
            {
                if (firstRender)
                    JsRuntime.InvokeVoidAsync("blazorjs.dragable");
    
                return base.OnAfterRenderAsync(firstRender);
            }
    
        }       

您可以在任何JavaScript(.js)文件中或在脚本标记中写入

<script>
    window.blazorjs = {
         dragable: function () {
            dragElement(document.getElementById("mydiv"));
    
            function dragElement(elmnt) {
                var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
                if (document.getElementById(elmnt.id + "header")) {
                    /* if present, the header is where you move the DIV from:*/
                    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
                } else {
                    /* otherwise, move the DIV from anywhere inside the DIV:*/
                    elmnt.onmousedown = dragMouseDown;
                }
    
                function dragMouseDown(e) {
                    e = e || window.event;
                    e.preventDefault();
                    // get the mouse cursor position at startup:
                    pos3 = e.clientX;
                    pos4 = e.clientY;
                    document.onmouseup = closeDragElement;
                    // call a function whenever the cursor moves:
                    document.onmousemove = elementDrag;
                }
    
                function elementDrag(e) {
                    e = e || window.event;
                    e.preventDefault();
                    // calculate the new cursor position:
                    pos1 = pos3 - e.clientX;
                    pos2 = pos4 - e.clientY;
                    pos3 = e.clientX;
                    pos4 = e.clientY;
                    // set the element's new position:
                    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
                    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
                }
    
                function closeDragElement() {
                    /* stop moving when mouse button is released:*/
                    document.onmouseup = null;
                    document.onmousemove = null;
                }
            }
        }
    }
    </script>

您可以在blazorfiddle中检查代码,https://blazorfiddle.com/s/uwjknuwx

我刚从w3schools复制并以炽烈的https://www.w3schools.com/howto/howto_js_draggable.asp

实施
相关问题