通过...滑动例子. [英] sliding examples through ....

查看:54
本文介绍了通过...滑动例子.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在单击按钮时移动文本.请帮助我,我无法执行此操作.

how to move text on button click. Please help me I am not able to do this.

推荐答案

我通常使用的方法是确保文本位于具有其位置的div中设置为绝对. div具有这种样式后,您可以通过设置元素样式的left和top属性来定位它.

在这里,我将一个按钮和一些文本包装在div中.单击该按钮将调用处理程序.处理程序将提供被单击的元素.在处理程序中,确定单击对象的父对象.从此处开始,将父元素(div)的左侧和顶部设置为随机值.

右移零位代码确保值是整数.
intValue = floatValue>> 0;

The way I usually use is to make sure the text is in a div that has it''s position set to absolute. Once the div has this style, you can position it by setting the left and top attributes of the element''s style.

Here, I''ve wrapped a button and some text in a div. Clicking the button calls a handler. The handler is supplied with the element that was clicked. Inside the handler, the clicked object''s parent is determined. From there, the left and top of the parent element (the div) are set to random values.

The ''shift-right zero places'' code makes sure the values are integers.
intValue = floatValue >> 0;

<!doctype html>
<html>
<head>
<style>
#tgtDiv
{
	border: solid 1px red;
	background-color: #ccc;
	display: inline-block;
	position: absolute;
}
</style>
<script>
function onBtnClick(clickedElement)
{
	var btnObj = clickedElement;
	var parentDiv = btnObj.parentNode;
	
	parentDiv.style.left = ((Math.random() * 320) >> 0) + "px";
	parentDiv.style.top = ((Math.random() * 200) >> 0) + "px";
}

</script>
</head>
<body>
	<div id='tgtDiv'>
		Click button to move to random position
		<button onclick='onBtnClick(this);'>Ok</button>
	</div>
</body>
</html>


这篇关于通过...滑动例子.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆