简体   繁体   English

更改style.top时图像不动

[英]Image not moving when style.top is changed

When I execute that style.top statement, the image doesn't want to change 600 px from the top. 当我执行style.top语句时,图像不想从顶部更改600 px。

 document.getElementById("testing").onclick = function(event){ document.getElementById("image").width=400; document.getElementById("image").style.top = "600px"; } 
 #testing{ color:blue; } 
 <p id="testing"> aewrfafffffffffffffffacvfav </p> <img id="image" src="katakana.jpg" alt="nothing" width="300"/> 

From my understanding, that should work. 据我了解,这应该起作用。 I don't know what's going on. 我不知道这是怎么回事。 In a nutshell, how can I change the position of an image with JavaScript? 简而言之,如何使用JavaScript更改图像的位置? There's the position absolute thing, but not sure. 有绝对立场的事情,但不确定。

The top property by itself does absolutely nothing. top属性本身绝对不执行任何操作。 The elements needs to be positioned as well. 元素也需要定位。 For example, position: relative or position: absolute . 例如, position: relativeposition: absolute

The top, right, bottom, and left properties specify the position of positioned elements. top,right,bottom和left属性指定定位元素的位置。

Source: https://developer.mozilla.org/en-US/docs/Web/CSS/position 来源: https//developer.mozilla.org/en-US/docs/Web/CSS/position

An example where the image is positioned relative to the container and the top property is changed after clicking the paragraph: 单击该段落后,图像相对于容器定位并且top属性更改的示例:

 document.getElementById("testing").onclick = function(event) { document.getElementById("image").style.top = "100px"; } 
 .container { position: relative; } img { position: absolute; width: 400px; } 
 <div class="container"> <p id="testing">aewrfafffffffffffffffacvfav</p> <img id="image" src="http://lorempixel.com/g/400/200/" /> </div> 

The top, right, bottom, and left properties specify the position of positioned elements. top,right,bottom和left属性指定定位元素的位置。 - position MDN - 位置MDN

Your image element is not positioned, and as a result using top, right, bottom, or left will have no effect. 您的图片元素定位,因此使用top,right,bottom或left无效。 In order to position an element without altering the flow of the document (which using fixed or absolute will do) you can use position: relative; 为了在不更改文档流的情况下定位元素(使用固定或绝对操作会如此),可以使用position: relative; and it will remain in the document flow while now being considered "positioned". 它将保留在文档流中,而现在被认为是“定位”。

 document.getElementById("testing").onclick = function(event){ document.getElementById("image").width=400; document.getElementById("image").style.top = "600px"; } 
 #testing{ color:blue; } #image{ position: relative; /* <- positioning */ } 
 <p id="testing"> aewrfafffffffffffffffacvfav </p> <img id="image" src="katakana.jpg" alt="nothing" width="300"/> 

What Is Positioning? 什么是定位?
By default, elements flow one after another in the same order as they appear in the HTML source, with each element having a size and position that depends on the type of element, the contents of the element, and the display context for the element as it will render on the page. 默认情况下,元素按照它们在HTML源代码中出现的顺序依次流动,每个元素的大小和位置取决于元素的类型,元素的内容以及该元素的显示上下文。它将呈现在页面上。 This default flow model for HTML layout doesn't allow a high level of control over the placement of elements on the page. HTML布局的默认流模型不允许对页面上元素的位置进行高度控制。 By applying a small set of CSS attributes to the elements that are defined for the page, CSS can control the precise position of elements by giving exact coordinates. 通过将少量CSS属性应用于为页面定义的元素,CSS可以通过提供精确的坐标来控制元素的精确位置。 - About Element Positioning MSDN - 关于元素定位 MSDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM