gpt4 book ai didi

javascript - 如何链接 javascript 和 html 来调整大小?

转载 作者:行者123 更新时间:2023-12-03 03:01:09 25 4
gpt4 key购买 nike

我是 javascript 新手,正在尝试遵循调整大小教程 here .

我通过直接复制粘贴演示在同一个文件夹中创建了三个文件,其中index.js和style.css。以下是index.html、index.js和style.css。 html和js文件似乎无法交互。

编辑:

interact('.resize-drag')
.draggable({
onmove: window.dragMoveListener
})
.resizable({
preserveAspectRatio: true,
edges: { left: true, right: true, bottom: true, top: true }
})
.on('resizemove', function (event) {
var target = event.target,
x = (parseFloat(target.getAttribute('data-x')) || 0),
y = (parseFloat(target.getAttribute('data-y')) || 0);

// update the element's style
target.style.width = event.rect.width + 'px';
target.style.height = event.rect.height + 'px';

// translate when resizing from top or left edges
x += event.deltaRect.left;
y += event.deltaRect.top;

target.style.webkitTransform = target.style.transform =
'translate(' + x + 'px,' + y + 'px)';

target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
target.textContent = Math.round(event.rect.width) + '×' + Math.round(event.rect.height);
});
.resize-drag {
background-color: #29e;
color: white;
font-size: 20px;
font-family: sans-serif;
border-radius: 8px;
padding: 20px;
margin: 30px 20px;

width: 120px;

/* This makes things *much* easier */
box-sizing: border-box;
}

.resize-container {
width: 100%;
height: 240px;
}
	<html>
<head>
<script type="text/javascript" src="index.js"></script>

<link rel="stylesheet" href="style.css"/>
</head>

<body>
<div class="resize-container">
<div class="resize-drag">
Resize from any edge or corner
</div>
</div>

</body>
</html>

最佳答案

正如评论中所述,它在没有可拖动功能的情况下工作。您可以看到它的实际效果

interact('.resize-drag')
.draggable({
onmove: window.dragMoveListener
})
.resizable({
preserveAspectRatio: true,
edges: { left: true, right: true, bottom: true, top: true }
})
.on('resizemove', function (event) {
var target = event.target,
x = (parseFloat(target.getAttribute('data-x')) || 0),
y = (parseFloat(target.getAttribute('data-y')) || 0);

// update the element's style
target.style.width = event.rect.width + 'px';
target.style.height = event.rect.height + 'px';

// translate when resizing from top or left edges
x += event.deltaRect.left;
y += event.deltaRect.top;

target.style.webkitTransform = target.style.transform =
'translate(' + x + 'px,' + y + 'px)';

target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
target.textContent = Math.round(event.rect.width) + '×' + Math.round(event.rect.height);
});
.resize-drag {
background-color: #29e;
color: white;
font-size: 20px;
font-family: sans-serif;
border-radius: 8px;
padding: 20px;
margin: 30px 20px;

width: 120px;

/* This makes things *much* easier */
box-sizing: border-box;
}

.resize-container {
width: 100%;
height: 240px;
}
	<html>
<head>
<script type="text/javascript" src="index.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/interact.js/1.2.9/interact.min.js"></script>
<link rel="stylesheet" href="style.css"/>
</head>

<body>
<div class="resize-container">
<div class="resize-drag">
Resize from any edge or corner
</div>
</div>

</body>
</html>

关于javascript - 如何链接 javascript 和 html 来调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47380830/

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