javascript - JS 使用不同的填充

标签 javascript php html css

我的网站有问题。 div class="row slide-content"的 padding-top 表现得很奇怪。 对于某些幻灯片,它约为 223 像素,而对于其他幻灯片,它使用不同的填充。

PHP

<div class="home-slider">
            <ul class="slides">

                <?php while ($slide =& $loop->next()): ?>

                    <li>
                        <div class="row slide-content">
                            <div class="large-10 large-centered columns text-center">                                                             
                                                      <?php if ( ! empty( $slide->button ) ) : ?>


                                        <div class="SLIDERLOGO"><?php echo $slide->button; ?> </div>


                                <?php endif; ?>


                                <?php if ( ! empty( $slide->ititle ) || ! empty( $slide->caption ) ) : ?>

                                    <div class="hero-title">
                                        <div class="large-3 columns nopad border-bottom"></div>
                                        <div class="large-6 columns text-center">

                                            <?php if ( ! empty( $slide->ititle ) ) : ?>

                                                <h4 class="text-white alt-h"><?php echo $slide->ititle; ?></h4>

                                            <?php endif; ?>

                                        </div>

                                        <?php if ( empty( $slide->ititle ) ) : ?>

                                                <div class="large-6 columns nopad border-bottom"></div>

                                        <?php endif; ?>

                                        <div class="large-3 columns nopad border-bottom"></div>
                                        <h1 class="text-white"><?php echo $slide->caption; ?></h1>
                                    </div><!--end of hero title-->

                                <?php endif; ?>



                            </div>
                        </div>

                        <img class="slider-bg" alt="<?php echo esc_attr( $slide->alt ); ?>" src="<?php echo esc_attr( $slide->img ); ?>" />
                    </li>

                <?php endwhile; ?>


            </ul>
        </div><!--end of Home slider-->

标志在这里插入

<?php echo $slide->button; ?>

两个标志的尺寸完全相同

JS //附加 HTML 作为幻灯片的 CSS 背景 //也使幻灯片的内容居中

jQuery('.home-slider .slides li').each(function () {

    var imgSrc = jQuery(this).children('.slider-bg').attr('src');
    jQuery(this).css('background', 'url("' + imgSrc + '")');
    jQuery(this).children('.slider-bg').remove();

    var slideHeight = jQuery(this).height();
    var contentHeight = jQuery(this).children('.slide-content').height();
    var padTop = (slideHeight / 2) - (contentHeight / 2);

    jQuery(this).children('.slide-content').css('padding-top', padTop);

});

这是我网站的链接 http://timberlife.nl

它不会每次都发生,但有时 Logo 和页面顶部之间的填充会发生变化。我希望我已经解释得足够好了:)

非常感谢! 大安

最佳答案

我已经在调试器打开的情况下运行了代码,并且可以将填充设置为 -8 像素。我认为这是因为 jQuery document.ready 函数不会等待图像下载,因此您的 li 元素没有您期望的高度。

您能否将您的 jQuery 函数包装在以下代码中,看看等到图像加载完毕是否可以解决问题?

$(window).bind("load", function() {
}

关于javascript - JS 使用不同的填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29015607/

相关文章:

php - JQuery .load() 从可排序列表中删除 .sortable() 函数

html - 在 flex 列中并排对齐 2 个元素

javascript - 对 AJAX XMLHttpRequest 感到困惑

javascript - canjs 模型上的更多 restfunctions

Aptana 中的 PHP - 函数声明?

html - 如果在祖 parent 之外溢出子元素?

html - 在 PrimeNG 中自定义按钮样式

javascript - 是否有类似模板文字的东西,反之亦然?

Javascript解析json对象

PHP/MySQL 更新语句插入一个 0?