简体   繁体   中英

Horizontal image slider not working in chrome and IE11

I am building a horizontal image slider and it is not working in Chrome and IE11. The weird part about it is that it does work in a fiddle while using Chrome and IE.

Here is the fiddle: https://jsfiddle.net/7y81hjtx/6/

And here it is on the web: http://ab-smith.com/wedding/slider.html

Notice how all the images to the right instead of scrolling, get super tiny to fit the screen?

here is the relevant HTML

<div class="galleryWrap" id="wrapper">
    <table border="0"> 
        <tbody>
            <tr>
                <td>
                    <div class="horizontalGalleryImageHolder">
                        <img id="1" src="http://lorempixel.com/200/200/" style="max-height: 390px;" />
                    </div>
                </td>
                <td>
                    <div class="horizontalGalleryImageHolder">
                        <img id="2" src="http://lorempixel.com/g/200/200/" style="max-height: 390px;" />
                    </div>
                </td>
                <td class="wide">
                    <div class="horizontalGalleryImageHolder">
                        <img id="3"src="http://lorempixel.com/400/200/sports/" style="max-height: 390px;" />
                    </div>
                </td>
                <td>
                    <div class="horizontalGalleryImageHolder">
                        <img id="4" src="http://lorempixel.com/200/200/sports/1/" style="max-height: 390px;" />
                    </div>
                </td>
                <td>
                    <div class="horizontalGalleryImageHolder">
                        <img id="5" src="http://lorempixel.com/200/200/" style="max-height: 390px;" />
                    </div>
                </td>
                <td>
                    <div class="horizontalGalleryImageHolder">
                        <img id="6" src="http://lorempixel.com/g/200/200/" style="max-height: 390px;" />
                    </div>
                </td>
                <td>
                    <div class="horizontalGalleryImageHolder">
                        <img id="7" src="http://lorempixel.com/200/200/sports/" style="max-height: 390px;" />
                    </div>
                </td>
                <td>
                    <div class="horizontalGalleryImageHolder">
                        <img id="8" src="http://lorempixel.com/200/200/sports/1/" style="max-height: 390px;" />
                    </div>
                </td>
                <td>
                    <div class="horizontalGalleryImageHolder">
                        <img id="9" src="http://lorempixel.com/200/200/" style="max-height: 390px;" />
                    </div>
                </td>
                <td>
                    <div class="horizontalGalleryImageHolder">
                        <img id="10" src="http://lorempixel.com/g/200/200/" style="max-height: 390px;" />
                    </div>
                </td>
                <td>
                    <div class="horizontalGalleryImageHolder">
                        <img id="11" src="http://lorempixel.com/200/200/sports/" style="max-height: 390px;" />
                    </div>
                </td>
                <td>
                    <div class="horizontalGalleryImageHolder">
                        <img id="12" src="http://lorempixel.com/200/200/sports/1/" style="max-height: 390px;" />
                    </div>
                </td>
            </tr> 
        </tbody>
    </table>
</div>

CSS

.ulWrapper {
margin: 20px auto 0;
display: block;
height: 50px;
}

.ulWrapper ul {
margin: 0 auto;
padding: 0;
display: table;
list-style: none;
}

.ulWrapper li {
float: left;
}
ul
{
list-style-type: none;
}

li{width: 50px; float: left; margin-right: 1px; margin-left: 1px;}
body {
    margin: 0;
}
table {
    border-collapse: collapse;
}
td.wide{width: 40%;}
td {
    width: 20%;
    padding: 0px;
}
img {
    width: 100%
}
.galleryWrap {
    width: 100%;
    overflow-x: scroll;
}
.left {
    float: left;
    width: 100px;
    height: 30px;
    background-color: blue;
    color: white;
}
.right {
    float: right;
    width: 100px;
    height: 30px;
    background-color: blue;
    color: white;
}

table {
    white-space: nowrap;
}
td {
    display: inline-block;
}

Javascript

 function left() {
        var width = document.getElementById('wrapper').clientWidth;
        var offsetWidth = width / 5; 

        document.getElementById("wrapper").scrollLeft -= offsetWidth;
    }

    function right() {
        var width = document.getElementById('wrapper').clientWidth;
        var offsetWidth = width / 5;

        document.getElementById("wrapper").scrollLeft += offsetWidth;
    }

and JQuery (although I am pretty positive this is ok)

$('.small-img').click(function () {
    var id = $(this).attr('id');
    var idNum = id.charAt(1);
    if (id.charAt(2) == '0'){
        idNum = 10;
    }else if(id.charAt(2) == '1'){
       idNum = 11; 
    }else if(id.charAt(2) == '2'){
       idNum = 12; 
    }
    document.getElementById(idNum).scrollIntoView();
});

How do I get the scroll bar back and keep the images from getting tiny?

The issue is that it doesn't like your images at 100% width. I removed this and it started working:

img {
  width: 100%;
}

I then replaced the 100% width with a fixed 100px width and it still worked.

I am not sure exactly why this is. Maybe the browser doesn't like trying to work out what should be going into overflow with % sizes?

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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