gpt4 book ai didi

angular - 拖动以在页面周围移动组件?

转载 作者:太空狗 更新时间:2023-10-29 19:29:44 24 4
gpt4 key购买 nike

我正在定义一个弹出对话框组件,它允许用户输入一些数据以放置在页面上。最终结果是当点击屏幕上的按钮时它会出现,并且还可以在页面上拖动。

我还没有太多这个组件,这是它的代码:

//edit-global-names-dialog-box.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'edit-global-names-dialog-box',
templateUrl: './edit-global-names-dialog-box.component.html',
styleUrls: ['./edit-global-names-dialog-box.component.css']
})
export class EditGlobalNamesDialogBoxComponent implements OnInit{

constructor() {}

ngOnInit() {
}
}

//edit-global-names-dialog-box.component.html

<div id="dialog-box-container">
<div id="header">

</div>
<div id="content">

</div>
<div id="footer">

</div>
</div>

//edit-global-names-dialog-box.component.css

#dialog-box-container {
height: 12%;
width: 25%;
z-index: 2 !important;
position: absolute;
background-color: lightgrey;
right: 50%;
bottom: 50%;
transform: translate(45%,-50.1%);
-moz-box-shadow: 8px 8px 8px #d9d9d9;
-webkit-box-shadow: 8px 8px 8px #d9d9d9;
box-shadow: 8px 8px 8px #d9d9d9;
}

#header {
border: 0.5px solid dimgrey;
border-bottom: none;
height: 20%;
}

#content {
border: 0.5px solid dimgrey;
border-bottom: none;
height: 50%;
}

#footer {
border: 0.5px solid dimgrey;
height: 26%;
}

你可以看到它目前只是一个模板,一个出现在所有其他页面内容之上的 div,有一个阴影并位于页面的中心。

我现在想要实现一个功能,当用户点击并拖动对话框的 header div 时,整个对话框会在页面上移动。

我看了之前发布的几个问题: Using JS to move a div around the page Make Div Draggable using CSS这些建议纯 javascript 或 JQuery 做事的方式,我不确定是否能与 Angular 很好地融合。

如何让我的组件在页面上四处拖动?

最佳答案

好吧,这是 promise 的使用 Javascript 的可拖动弹出窗口的示例。请注意,我还为此使用了 JQuery 以使其更容易一些,但使用的所有函数本质上只是“常规”Javascript 函数的包装器,因此可以很容易地将其转换为“常规”javascript,我只是不能'不要被打扰。 :)

如果您在 ID 为“myPopup”的 div 中创建您的内容,只需将“myPopup”ID 更改为您想要的任何内容,假设您有可用的 jquery,您几乎可以复制粘贴此内容。 Ofc 有改进它的方法,但这应该让你开始。

function displayPopup() {
$("#myPopup").toggleClass("popupVisible");
}

$(document).ready(function() {

var isDragging = false;
$("#myPopup")
.mousedown(function() {
isDragging = false;
$("#myPopup").addClass("clicked")
})
.mousemove(function() {
isDragging = true;
if($("#myPopup").hasClass("clicked")) {
$("#myPopup").css("left", event.pageX - 20);
$("#myPopup").css("top", event.pageY - 20);
}
})
.mouseup(function() {
var wasDragging = isDragging;
isDragging = false;
if (!wasDragging) {

}
$("#myPopup").removeClass("clicked")
});


});
<html>  
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<style>
#myPopup {
position: absolute;
width: 5cm;
height: 5cm;
background: #00ff00;
top: calc(50% - 2.5cm);
left: calc(50% - 2.5cm);
display: none;
}
.popupVisible {
display: block !important;
}
</style>
</head>
<body>
<a href="#" onclick="displayPopup()">Here's the popup!</a>

<div id="myPopup">
</div>

</body>
</html>

关于angular - 拖动以在页面周围移动组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42334722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com