简体   繁体   English

如果动态图像与javascript垂直,则尝试使其居中

[英]Trying to center dynamic image if it's vertical with javascript

I was looking for some solutions on this but I can't seem to figure it out. 我一直在寻找有关此问题的解决方案,但似乎无法解决。 I am trying to center an image horizontally on the page when it's a vertical image, so basically getting the dimensions of the image, and then inserting CSS to center it (if it's vertical). 当它是垂直图像时,我试图将图像水平放置在页面上,因此基本上可以获取图像的尺寸,然后插入CSS以使其居中(如果是垂直)。 Here's my markup with a screen shot. 这是我的截图截图。 One is correct, the other is not. 一个是正​​确的,另一个是不正确的。 Thank you!! 谢谢!! 在此处输入图片说明 在此处输入图片说明

      <div class="product-image large">
        <img data-bind="attr:{alt:currentSampleImage, src:servicePath+'products/'+product().id+'/images/sample/_'+currentSampleImage()+'/large?auth='+ax.JRR}" id="productImage" src="https://dev.axsmb.com/api/products/26/images/sample/_0/large?auth=sov9fbd75cgubtfhoq4o47kj3tr2dn4fbl90pi54on05vqpc83p1" alt="0" >
      </div>

      <div data-bind="owlCarousel: { data: sampleImageKeys, owlOptions: carouselSettings }" class="owl-carousel carousel beta owl-theme" style="opacity: 1; display: block;">
          <div class="owl-wrapper-outer"><div class="owl-wrapper" style="width: 194px; left: 0px; display: block; transition: all 0ms ease 0s; transform: translate3d(0px, 0px, 0px);"><div class="owl-item" style="width: 97px;"><div class="thumbnail product-image small">
            <img src="https://dev.axsmb.com/api/products/26/images/sample/_0/small?auth=sov9fbd75cgubtfhoq4o47kj3tr2dn4fbl90pi54on05vqpc83p1" data-bind="click:$root.selectImage.bind($data), attr:{rel:$index, alt:$index, src:$root.servicePath+'products/'+$root.product().id+'/images/sample/_'+$index()+'/small?auth='+ax.JRR}" rel="0" alt="0" >
          </div></div></div></div>
      <div class="owl-controls clickable" style="display: none;"><div class="owl-pagination"><div class="owl-page"><span class=""></span></div></div><div class="owl-buttons"><div class="owl-prev"><button class="nav nav-prev"><i class="fa fa-caret-left"></i><span>Previous</span></button></div><div class="owl-next"><button class="nav nav-next"><span>Next</span><i class="fa fa-caret-right"></i></button></div></div></div></div>

    </div>

Well here's a jsfiddle i put together https://jsfiddle.net/nponz4dc/13 You may need to tweak it a little to get it to work with your project but i think its a good start. 好吧,这是我组装的jsfiddle https://jsfiddle.net/nponz4dc/13您可能需要对其进行一些微调才能使其与您的项目一起使用,但是我认为这是一个良好的开端。 Fiddle 小提琴

HTML 的HTML

You've got two products, one with a vertical image and one with a horizontal 您有两种产品,一种具有垂直图像,另一种具有水平图像

<div class="product">
  <img src="https://usercontent1.hubstatic.com/8573904.jpg">
</div>
<div class="product">
  <img src="http://www.mathopenref.com/images/lines/horizontal.png">
</div>

CSS 的CSS

Here's some basic css, just using text-align. 这是一些基本的CSS,仅使用text-align。 You may need more styles like margins or displays. 您可能需要更多样式,例如边距或显示。

.product {
  background: red;
}

.horizontal {
  text-align: center;
}

JS JS

Basically, i'm comparing the images height to it's width and then if its height is greater than it's width i'm adding a class to it's parent called "horizontal" that has the appropriate styles. 基本上,我将图像的高度与其宽度进行比较,然后,如果其高度大于宽度,则向其父级添加一个具有适当样式的类,称为“水平”。

var images = document.getElementsByTagName("img");
//you might want to target only the images you need
//and not the all of the images in the DOM
for (var i = images.length - 1; i >= 0; i--) {
  var parent = images[i].parentNode;
  if (images[i].height > images[i].width) {
    parent.classList.add("horizontal");
  }
};

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

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