简体   繁体   English

<div>间接更改大小时不会触发转换

[英]<div> transition not triggering when size is changed indirectly

I have a section in my site where four different promotion thumbs are displayed.我的网站中有一个部分,其中显示了四种不同的促销拇指。

There is an info div that shows more info about the promotion.有一个信息 div 显示有关促销的更多信息。

Thumbs have an onclick that call a function in javascript that updates the current displayed text in the info div. Thumbs 有一个 onclick,它调用 javascript 中的一个函数来更新信息 div 中当前显示的文本。

I have state in the CSS that I desire a 0.3 transition for the info div, it is applied but it does not trigger of from the change of text in the div.我在 CSS 中有状态,我希望信息 div 有一个 0.3 转换,它被应用,但它不会从 div 中的文本更改中触发。 Naturally some promos have more content than others.自然,一些宣传片比其他宣传片有更多的内容。

My idea with this is that I want the info div to transition height when looking thru promotions, instead of just flat change in height.我的想法是,我希望信息 div 在查看促销时转换高度,而不仅仅是高度的平面变化。

The site is live and works.该网站是现场和工作。 Here is a direct link to the page with the promos thumbs.这是带有促销拇指的页面的直接链接。

LINK >>>>>>> http://thezone.bg/promo <<<<<<<< LINK链接 >>>>>>> http://thezone.bg/promo <<<<<<<< 链接

I wouldn't like to sophisticate it too much, I aim for a simple solution to the problem.我不想太复杂,我的目标是解决问题的简单方法。

I would like to thank in advance for your time and effort in solving this problem.我要提前感谢您为解决这个问题付出的时间和精力。

<p class="roboto_big">Промоции</p>
<div class="image"><img src="uploads/images/NT-Test/promos.png" alt="" width="100%"></div>
<br>
<p class="roboto">Предлагаме многобройни промоции за всички наши клиенти. Възползвайте се от интернет и телевизия, които не са само услуги а начин на живот, на 110% с отстъпки и бонусите предлагани от Зоната.</p>
<hr class="horizontal">
<table style="border-spacing: 60px 0px;">
  <tbody>
    <tr>
      <td style="text-align: center;">
        <p class="roboto_medium">Колкото повече, толкова повече!</p>
        <img class='promo_thumb' onclick="more()" src="uploads/images/NT-Test/Router_Equalizer.png" alt="Промоция, 'Колкото повече толкова повече' ">        
      </td>
      <td style="text-align: center;">
        <p class="roboto_medium">Доведи <br> приятел</p>
        <img class='promo_thumb' onclick="month()" src="uploads/images/NT-Test/free-month2.png" alt="Промоция 'Доведи приятел'">
      </td>
      <td style="text-align: center;">
        <p class="roboto_medium">Лоялен <br> клиент</p>
        <img class='promo_thumb' onclick="client()" src="uploads/images/NT-Test/loyal-client.png" alt="Промоция 'Лоялен клиент'">
      </td>
      <td style="text-align: center;">
        <p class="roboto_medium">Диема <br> Екстра</p>
        <img class='promo_thumb' onclick="DiemaXtra()" src="uploads/images/NT-Test/diema-extra-the-zone.png" alt="Промоция 'Диема Екстра'">
      </td>
    </tr>
  </tbody>
</table>
<div id="promo_info" class="promo">
  <span id="promo_title" class="roboto_big_white" style="color: white;">Колкото повече, толкова повече!</span><hr class="horizontal" style="opacity: 0;">
  <p id="promo_content" class="roboto_white">Тази оферта е за всеки клиент подписал договор за услуга над 30Мб/с за целия срок на договора. Предоставяме ви 300 Мегабитов безжичен рутер за ползване през времето за което сте наш абонат. Настройката на рутера е безплатна и се извършва с характеристики, пожелани от Вас. Рутерът работи с новия 802.11n стандарт, поддържащ няколко антени на едно устройство което подобрява обсега на рутера, скоростта с която може да се прехвъря информация и броя на абонати свързани към домашната мрежа. Също така, ако включите телевизионен пакет Comfort TV/Mania TV получавате безплатно второ цифрово устройство !</p>
</div>

CSS -------------------------- CSS --------------------------

div.promo {
  width: 90%;
  height: auto;
  background-color: #ba050a;
  background-image: url(http://thezone.bg/uploads/images/NT-Test/red_pane.png);
  background-repeat: no-repeat;
  margin: 0px auto;
  margin-top: 50px;
  padding: 10px;
  border: 2px solid #ba050a;
  border-radius: 5px 5px 0px 0px;
  box-shadow: 0px 0px 7px black;
  -webkit-transition: all 0.3s ease-in-out;
  -moz-transition: all 0.3s ease-in-out;
  -ms-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
}

.promo_thumb {
  cursor: pointer;
  position: relative;
  width: 100%;
}

You can't use css transitions with height: auto .您不能使用具有height: auto css 过渡。 So, the solution is to manually set the height.所以,解决办法是手动设置高度。

This function will measure the height of the container and the content before the transition, then measure the height of the content after the transition to get the difference, and add that difference to the container height.此函数将测量容器和过渡前内容的高度,然后测量过渡后内容的高度以获得差异,并将该差异添加到容器高度。 Thus triggering the transition.从而触发过渡。

function showPromotion(titleHTML, contentHTML) {
    var height = parseInt(getComputedStyle(info).height, 10);
    var start = content.scrollHeight;
    title.innerHTML = titleHTML;
    content.innerHTML = contentHTML;
    var diff = content.scrollHeight - start;
    info.style.height = (height + diff) + "px";
}

To detail gilly's comment, transition effects require a "from" number, and a "to" number.为了详细说明gilly 的评论, transition效果需要一个“from”数字和一个“to”数字。 You can't transition height based on the total amount of text appearing.您不能根据出现的文本总量来转换高度。 (This would actually also need to account for resize operations, and perhaps some other cases) (这实际上还需要考虑调整大小操作,也许还有其他一些情况)

The best way I've gotten something like this working is this.我得到这样的工作的最好方法是这样。 No pseudocode, sorry:没有伪代码,抱歉:

  1. Save current clientHeight of div.保存当前 div 的 clientHeight。
  2. Manually apply that height to the div as a style.将该高度作为样式手动应用到 div。
  3. Apply text change应用文本更改
  4. Save the scrollHeight of the div (total height, ignoring your style change).保存 div 的 scrollHeight(总高度,忽略您的样式更改)。
  5. In a setImmediate or setTimeout call, set the div's height to be the height from step 3.setImmediatesetTimeout调用中,将 div 的高度设置为步骤 3 中的高度。

You may need to call something similar if the user resizes the browser and the height needs to change when everything auto-adjusts.如果用户调整浏览器大小并且高度需要在一切自动调整时更改,您可能需要调用类似的方法。

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

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