gpt4 book ai didi

javascript - 如何使用单选按钮将图像移动到屏幕上的不同位置?

转载 作者:太空宇宙 更新时间:2023-11-04 05:44:30 25 4
gpt4 key购买 nike

//What button was placed
function positionChoice (choice) {
var dom = document.getElementById("myForm");

// Determine which button was pressed

for (var index = 0; index < dom.choice.length;
index++) {
if (dom.choice[index].checked) {
choice = dom.choice[index].value;
break;
}
}



switch (choice) {
case "Northwest":
moveItNorthwest();
break;
case "Northeast":
moveItNortheast();
break;
case "Southeast":
moveItSoutheast();
break;
case "Southwest":
moveItSouthwest();
break;
default:
alert("Error in JavaScript function choice");
break;
}
}

//To move picture to specified place
function moveItNortheast(movee, newTop, newLeft) {

dom = document.getElementById().style;
if()

dom.top = 1200px;
dom.left = 10px;
}

function moveItNorthwest(movee, newTop, newLeft) {

dom = document.getElementById(movee).style;
if()

dom.top = 100px;
dom.left = 100px;
}

function moveItSoutheast(movee, newTop, newLeft) {

dom = document.getElementById(movee).style;
if()

dom.top = 1200;
dom.left = 80px;
}

function moveItSouthwest(movee, newTop, newLeft) {

dom = document.getElementById(movee).style;
if()

dom.top = 80px;
dom.left = 1200px;
}

这是我目前所做的,我的想法是选择选项,然后为每个选项(西北、东北、西南和东南)创建一个函数,并在它们被选中时调用它们中的每一个,这将移动图片到不同的地方。我不确定还能尝试什么,我尝试在网上寻找类似例子的东西,但没有任何帮助。

感谢任何帮助!

编辑:我根本无法移动图像,这就是我遇到的问题。

最佳答案

这是一个 jsFiddle 供您查看:https://jsfiddle.net/3n6jcpxe/9/希望这会有所帮助:)

OnStart();
function OnStart() {
console.log('Entered OnStart');

var rad = document.querySelectorAll('input');
var prev = null;

console.log(rad);

for (var i = 0; i < rad.length; i++) {
console.log('Adding event listeners');
rad[i].addEventListener('change', function() {
(prev) ? console.log(prev.value): null;
if (this !== prev) {
prev = this;
console.log(this.value);
PositionChoice(this.value);
}
});
}

};

//What button was placed
function PositionChoice(_index) {
console.log('Entered positionChoice: ' + _index);

/*var myForm = document.getElementById('myForm');

// Determine which button was pressed
for (var index = 0; index < myForm.choice.length; index ++) {
if (myForm.choice[index].checked) {
choice = myForm.choice[index].value;
break;
}
}*/

switch (_index) {
case 'Northwest':
Move('100px', '10px');
break;
case 'Northeast':
Move('120px', '20px');
break;
case 'Southeast':
Move('150px', '30px');
break;
case 'Southwest':
Move('160px', '40px');
break;
default:
alert("Error in JavaScript function choice");
break;
}
};

//To move picture to specified place
function Move(_newTop, _newLeft) {
console.log('Entered Move: ' + _newTop + ' ' + _newLeft);

myPic = document.getElementById('cloudpic').style;

myPic.top = _newTop;
myPic.left = _newLeft;
};
<!DOCTYPE html>
<html lang = "en">
<head>
<title> Question 1 </title>
<meta charset = "utf-8" />
<style type = "text/css">
.container1{
position:relative;
display: inline-block;
}

.text-pgh{
position:absolute;
color:black;
bottom:20px;
right:20px;
padding-left:20px;
padding-right:20px;
}

#cloudpic{
position: relative;

top: 0px;
left: 0px;

pointer-events: none;

opacity: 0.2;
}

</style>
</head>

<body>
<div class="container1">
<img id="cloudpic" src="https://picsum.photos/200/300" alt="Cloud 9 picture">

<div class="text-pgh">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
</div>


<form id="myForm">
<input type="radio" id="radio1" value= "Northwest" name="positions"/>Northwest<br>
<input type="radio" id="radio2" value= "Northeast" name="positions"/>Northeast<br>
<input type="radio" id="radio3" value= "Southeast" name="positions"/>Southeast<br>
<input type="radio" id="radio4" value= "Southwest" name="positions"/>Southwest<br>
</form>

</body>
</html>

关于javascript - 如何使用单选按钮将图像移动到屏幕上的不同位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58846001/

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