简体   繁体   English

水平div图像滚动动态宽度

[英]horizontal div images scroll dynamic width

i have a problem with my horizontal image scroll. 我的水平图像滚动出现问题。 There are many threats with some solutions but there is no solution that worked for me :( 有些解决方案有很多威胁,但没有对我有用的解决方案:(

Here is the link with my image gallery: http://lichtspielfotografie.com/portrait/ 这是我的图片库的链接: http : //lichtspielfotografie.com/portrait/

The css that i use: 我使用的CSS:

.photos {
         width:10000px;
         height:100%;   
         position:absolute;
         left:0;
         margin-left:0; 
         margin-top: 0; 
}
#scroll{ 
        position:absolute;  
        width:100%;   
        height:100%;  
        overflow-x:scroll; 
        overflow-y:hidden; 
        margin-left:0; 
        left:0;
        margin-top: -8,25%; }

And the html/php: 和html / php:

         <div id="scroll">
            <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
            <script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
            <script type="text/javascript">
                    $(document).ready(function() {
                    $('html, body, *').mousewheel(function(e, delta) {
                    this.scrollLeft -= (delta * 20);
                    e.preventDefault();
                    });
                });
            </script>

            <div class="photos">
                <?php global $post; 
                $src = '';
                $breite='0px';
                $args = array( 
                'post_type' => 'attachment', 
                'numberposts' => -1, 
                'post_mime_type' => 'image', 
                'post_status' => null, 
                'post_parent' => $post->ID );
                $attachments = get_posts($args); ?>

                <?php
                if ($attachments) {
                    foreach ( $attachments as $attachment ) {
                        $src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
                        if ($src) {
                            echo '<img src='.$src[0];'';
                            } 
                        echo ' height="100%"/img> &nbsp;';                          
                    }}
                ?>    
           </div> 
        </div>
    </main><!-- #main -->
</div><!-- #primary -->

I hope you can help me to geht the width of the photos dynamic. 希望您能帮助我动态调整照片的宽度。 I'm very frustrated with this problem. 我对这个问题感到非常沮丧。

You could try selecting all the images and the iterate through all of them, getting their width and then adding it to the container. 您可以尝试选择所有图像并遍历所有图像,获取它们的宽度,然后将其添加到容器中。 Also please note if you have some margin between elements(images) you should add that as well. 另外请注意,如果元素(图像)之间有一定的边距,则也应添加该边距。

// get all images in an html collection
var imgs = document.getElementsByTagName('img');
// initialize sum variable for holding all the images width sum
var sum = 0;
// iterate the html collection
for(var i = 0; i < imgs.length; i++) {
   // wait for images to load
   imgs[i].onload(function() {
      sum += imgs[i].width;
   });
}
// add the width to the parent element
document.getElementsByClassName('photos')[0].style.width = sum;

First of all practice more your css styling and html. 首先练习更多的CSS样式和html。 Remove the &nbsp from between your photos and put a margin between them. 删除照片之间的&nbsp并在照片之间留出一定的空白。 You can do something like: 您可以执行以下操作:

.photos img {
    margin: 0 2px;
}

in your css for example. 例如在您的CSS中。 Then to calculate the total width of your images you will need javascript. 然后,要计算图片的总宽度,您将需要使用javascript。

You need to loop through your images and get the width of each of them. 您需要遍历图像并获取每个图像的宽度。 If we assume that you took my advice and you have replaced the &nbsp with css styling (margin), we will use .outerWidth . 如果我们假设您接受了我的建议,并且已将&nbsp替换为css样式(边距),则将使用.outerWidth This will get the image width with it's paddings and border width. 这将获得带有边框和边框宽度的图像宽度。 If we put true like so: .outerWidth(true) it will get the margins too. 如果我们这样设置true: .outerWidth(true)它也会获得边距。 That's what we need. 那就是我们所需要的。 For looping through the images we will use .each() . 为了遍历图像,我们将使用.each()

Here we go: 开始了:

var totalWidth = 0;

$('.photos > img').each(function() {
    totalWidth += $(this).outerWidth(true);
});

Now we just need to set the new width to the images container: 现在我们只需要为图像容器设置新的宽度:

$('.photos').css('width', totalWidth + 'px');

If you have some other questions feel free to ask. 如果您还有其他问题,请随时提出。 Good luck :) 祝好运 :)

EDIT: 编辑:

You have to do this when your html loads. html加载时必须执行此操作。 You have the functionality already in your code. 您的代码中已有功能。 It's the $(document).ready(function() {}); 这是$(document).ready(function() {}); . Here you put all the javascript code that you want to be executed after the html loads. 在这里,您放置了要在html加载后执行的所有javascript代码。

So in this case you will do: 因此,在这种情况下,您将执行以下操作:

$(document).ready(function() {

    // ... other js code here ...

    var totalWidth = 0;

    $('.photos > img').each(function() {
        totalWidth += $(this).outerWidth(true);
    });

    // ... other js code here ...

});

You can also put your code into another js file and load it in your head below your js libraries like jQuery. 您还可以将代码放入另一个js文件,并将其加载到jQuery之类的js库下方的头中。

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

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