javascript - 移动图像和文本 - Javascript + html

标签 javascript html css

我有一个包含一些图像和文本的 HTML 页面,我被要求仅使用 javascript 重新开发它。到目前为止,我已经设法从两边翻转图像和文本,放入新的文本和标题。

我遇到的问题是图像歪斜,看起来不对。如何将图像插入以使文本和图像本身对齐。

HTML

<div class="article">
            <div id="myTills">
                <h3 id="headingTwo">About the Product</h3>
                <p id="paragraphTwo">
                    TigerTurf artificial chipping lawns and Golf Greens provide superior quality and ball roll. You can choose which type of synthetic grass you require for areas such as rough and tee boxes. Installing your practice green in your home Garden or business, will allow you to improve your game in your spare time. Your Golf Green will be smooth, durable and realistic to play on. If you have any more questions about design and installation, contact us anytime.
                </p>
            </div>
            <img id="pic4" src="images/nature-place-photo.png" alt="">
        </div>  

Javascript

var x = document.getElementById("pic4");
x.setAttribute("src", "../images/water_image2.png");
x.style.cssFloat = 'left';
x.style.width = "356px";
x.style.height = "250px";

var y = document.getElementById("headingTwo");
y.innerText = "Tills"
y.style.textAlign = "left"; 

var x = document.getElementById("paragraphTwo");
x.innerText = "Contains new text"
x.style.textAlign = "left";

var y = document.getElementById("myTills");
y.style.cssFloat = "right";

最佳答案

我对你的代码做了一些修改。在您的 HTML 中,我切换了 img#myTills 的位置,因为父元素的 displaytable。由于父元素的 displaytable,您可能希望 #myTills 的显示是 table-cell因此您可以使用 vertical-align: middle 使文本垂直居中对齐。

HTML

<div class="article">
    <img id="pic4" src="images/nature-place-photo.png" alt="">
    <div id="myTills">
        <h3 id="headingTwo">About the Product</h3>
        <p id="paragraphTwo">
            TigerTurf artificial chipping lawns and Golf Greens provide superior quality and ball roll. You can choose which type of synthetic grass you require for areas such as rough and tee boxes. Installing your practice green in your home Garden or business, will allow you to improve your game in your spare time. Your Golf Green will be smooth, durable and realistic to play on. If you have any more questions about design and installation, contact us anytime.
        </p>
    </div>
</div>

Javascript

var article = document.querySelector('.article');
article.style.display = 'table';

var x = document.getElementById("pic4");
x.style.width = "356px";
x.style.height = "250px";
x.style.display = "table-cell";

var y = document.getElementById("headingTwo");
y.innerText = "Tills"
y.style.textAlign = "left";

var x = document.getElementById("paragraphTwo");
x.innerText = "Contains new text"
x.style.textAlign = "left";

var y = document.getElementById("myTills");
y.style.display = "table-cell";
y.style.height = "250px";
y.style.width = "100%";
y.style.verticalAlign = "middle";

这就是您真正想要实现的目标吗?

关于javascript - 移动图像和文本 - Javascript + html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40824661/

相关文章:

javascript - Angular 1.2 基于类应用动画

html - 将不同的 CSS 应用于具有相同类的 div

javascript - 使用 Axios 克服 Pending Promise 并在 Node.js 中完成 JSON 构建

javascript - 与在 JavaScript 中单独定义变量相比,将变量与逗号一起定义有好处吗?

javascript - 我可以运行在禁用的复选框中显示工具提示的 .hover 吗?

javascript - 如何隐藏除第一个以外的所有div?

php - 将数据从 MySQL 数据库加载到下拉列表单击的文本框中

HTML/CSS : @media for exact screen size

html - 如何在所有浏览器的特定 div 中隐藏垂直滚动条

javascript - 更改 Bing map 元素中的 CSS?