iorgu iorgu - 4 months ago 22
jQuery Question

Unslider: Center images even on "low" screen resolution

I'm using Unslider (http://unslider.com/) in a project.

I succeeded to align the whole slider part a half way to the goal.
But the problem is that the slides on a 1920 and lower width resolution are not centered. The width of the images are 3940 px.

I tried to apply the code from this answer; like this:

$(function () {
$('.banner').unslider();
AdjustSliderImgWrapper();
});

$(window).resize(AdjustSliderImgWrapper);

function AdjustSliderImgWrapper() {
var left = (parseInt($('.banner > li > img').width()) - $('.banner > li').innerWidth()) / -2;
$('.banner > li > img').css('left', left);
}


, but no avail. I also tried:

$(".banner img").each(function () {
$(this).css("margin-left", -this.width / 2);
})


also:

background-position: center center !important;


Here is my code:



window.console && console.warn("This version of Unslider is due to be deprecated by December 1. Please visit unslider.com for details on how to upgrade."), function (t, s) {
if (!t)
return s;
var i = function () {
this.el = s,
this.items = s,
this.sizes = [],
this.max = [0, 0],
this.current = 0,
this.interval = s,
this.opts = {
speed : 500,
delay : 3e3,
complete : s,
keys : !s,
dots : s,
fluid : s
};
var i = this;
this.init = function (s, i) {
return this.el = s,
this.ul = s.children("ul"),
this.max = [s.outerWidth(), s.outerHeight()],
this.items = this.ul.children("li").each(this.calculate),
this.opts = t.extend(this.opts, i),
this.setup(),
this
},
this.calculate = function (s) {
var e = t(this),
n = e.outerWidth(),
h = e.outerHeight();
i.sizes[s] = [n, h],
n > i.max[0] && (i.max[0] = n),
h > i.max[1] && (i.max[1] = h)
},
this.setup = function () {
if (this.el.css({
overflow : "hidden",
width : i.max[0],
height : this.items.first().outerHeight()
}), this.ul.css({
width : 100 * this.items.length + "%",
position : "relative"
}), this.items.css("width", 100 / this.items.length + "%"), this.opts.delay !== s && (this.start(), this.el.hover(this.stop, this.start)), this.opts.keys && t(document).keydown(this.keys), this.opts.dots && this.dots(), this.opts.fluid) {
var e = function () {
i.el.css("width", Math.min(Math.round(i.el.outerWidth() / i.el.parent().outerWidth() * 100), 100) + "%")
};
e(),
t(window).resize(e)
}
this.opts.arrows && this.el.parent().append('<p class="arrows"><span class="prev">←</span><span class="next">→</span></p>').find(".arrows span").click(function () {
t.isFunction(i[this.className]) && i[this.className]()
}),
t.event.swipe && this.el.on("swipeleft", i.prev).on("swiperight", i.next)
},
this.move = function (s, e) {
this.items.eq(s).length || (s = 0),
0 > s && (s = this.items.length - 1);
var n = this.items.eq(s),
h = {
height : n.outerHeight()
},
o = e ? 5 : this.opts.speed;
this.ul.is(":animated") || (i.el.find(".dot:eq(" + s + ")").addClass("active").siblings().removeClass("active"), this.el.animate(h, o) && this.ul.animate(t.extend({
left : "-" + s + "00%"
}, h), o, function () {
i.current = s,
t.isFunction(i.opts.complete) && !e && i.opts.complete(i.el)
}))
},
this.start = function () {
i.interval = setInterval(function () {
i.move(i.current + 1)
}, i.opts.delay)
},
this.stop = function () {
return i.interval = clearInterval(i.interval),
i
},
this.keys = function (s) {
var e = s.which,
n = {
37 : i.prev,
39 : i.next,
27 : i.stop
};
t.isFunction(n[e]) && n[e]()
},
this.next = function () {
return i.stop().move(i.current + 1)
},
this.prev = function () {
return i.stop().move(i.current - 1)
},
this.dots = function () {
var s = '<ol class="dots">';
t.each(this.items, function (t) {
s += '<li class="dot' + (1 > t ? " active" : "") + '">' + (t + 1) + "</li>"
}),
s += "</ol>",
this.el.addClass("has-dots").append(s).find(".dot").click(function () {
i.move(t(this).index())
})
}
};
t.fn.unslider = function (s) {
var e = this.length;
return this.each(function (n) {
var h = t(this),
o = (new i).init(h, s);
h.data("unslider" + (e > 1 ? "-" + (n + 1) : ""), o)
})
}
}
(window.jQuery, !1);

* {
margin: 0;
padding: 0;
}
#outermin1 {
width : 100%;
}
.banner {
position : relative;
width : auto !important;
overflow : auto;
text-align : center;
}
/*
*/

.banner ul {
list-style : none;
width : 300%;
padding: 0px;
}
.banner ul li {
display : block;
float : left;
width : 33%;
padding : 0 0 110px;
min-height: 1550px !important;
max-height: 1550px !important;
}
.banner h1 {
position : relative;
margin : 0 auto;
height : 120px;
top : 30px;
text-align : center;
font-size : 250% !important ;
color : #FFF;
text-transform : uppercase;
font-weight : 100;
/*line-height : 38px !important ;*/
}
.banner h2 {
text-transform : none;
font-size : 150%;
top : -15px;
position : relative;
color : #FFF;
}
.banner .btn {
display : inline-block;
margin : 25px 0 0;
padding : 9px 22px 7px;
clear : both;
color : #fff;
font-size : 12px;
font-weight : 700;
text-transform : uppercase;
text-decoration : none;
border : rgba(255, 255, 255, 0.4) solid 2px;
border-radius : 5px;
}
.banner .btn:hover {
background : rgba(255, 255, 255, 0.05);
}
.banner .dots {
position: absolute;
left: 0px;
right: 0px;
bottom: -10px;
background: #fff;
height: 103px;
padding-top: 30px;
}
.banner .dots li {
display : inline-block;
width : 10px;
height : 10px;
margin : 0 4px;
text-indent : -999em;
border : #17b7a5 solid 2px;
border-radius : 6px;
cursor : pointer;
opacity : 0.4000000059604644775390625;
transition : background 0.5s, opacity 0.5s;
}
.banner .dots li.active {
background : #17b7a5;
opacity : 1;
}
/*
.banner img {
width: 100%;
margin-top: -143px;
}*/
.banner img {
background-size: cover;
z-index: 3;
}
/* this is the tryout part */
.banner > li { position: relative; }
.banner > li > img { position: absolute; top: 0; width: 3940px !important; text-align: center;}

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="favicon.ico">

<title>theimapcter.org</title>

<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="css/ie10-viewport-bug-workaround.css" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="css/banner.css" rel="stylesheet">
<link href="css/egyedi.css" rel="stylesheet"> <!-- a kialakitas -->


<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="js/unslider.min.js"></script>
<script type="text/javascript">
$(function() {
$('.banner').unslider({
speed: 500,
delay: 15000,
complete: function() {},
keys: true,
dots: true,
fluid: true
});
});

</script>
</head>

<body>

<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><img src="svg/impacter_logo_fejlec.svg" alt="logo"></a>
</div>
<div id="navbar" class="collapse navbar-collapse" >
<ul class="nav navbar-nav">
<li><a href="">Projects</a></li>
<li><a href="">Mindset</a></li>
<li><a href="">Make an impact</a></li>
<li><a href="">Search</a></li>
<li><a href="">Login / Register</a></li>
</ul>
</div>
</nav>
<div class="box" id="outermin1" style="z-index:4">
<!--banner-->
<div class="banner">
<ul>
<li style="background:#00b8a5;"><div class="slider-wrapper"><div class="slide-outer"><div class="slide-szoveg">
<h1 class="cim1">Do you have the ability to respond challenges?</h1><h2 class="cim2">In other words: how do you deal with responsibility?</h2><br/><div class="start-slide"><span class="start-slide1">Start</span><br/><span class="start-slide2">the responsibility quick TEST!</span></div><span class="join-slide">Join the social experiment</span>
<span class="use-slide">Use Impacter platform to develop new ways and solve actual social/environmental<br/>challenges responsibly – meanwhile learn about yourself.</span><span class="find-slide">Find your challenge</span></div></div><img src="https://dl.dropboxusercontent.com/s/8nop7364npk31ue/slide1.png" alt="lab" class="slide1" /></div>
</li>
<li style="background:#8fc453;"><div class="slider-wrapper"><div class="slide-outer"><div class="slide-szoveg">
<h1 class="cim3">Projects with real-life impact</h1><h2 class="cim3">Activate ideas to generate change </h2><br/><br/><span>Upload a challenge</span>&nbsp;<span>Join a project</span></div></div><img src="https://dl.dropboxusercontent.com/s/7l99ejj81bd2g1d/slide2.png" alt="World map" class="slide2" /></div>
</li>
<li style="background:#37c6ea;"><div class="slider-wrapper"><div class="slide-outer"><div class="slide-szoveg">
<h1>Develop new ways to solve actual social / environmental challenges</h1><br/><br/><h2><strong class="line2">– meanwhile learn about yourself.</strong></h2><span>Challenges</span><span>Taking<br/>the responsibility</span><span>Response</span><span>Make an impact</span></div></div><img src="https://dl.dropboxusercontent.com/s/geiezcd6hym6mxt/slide3.png" alt="people on pc" class="slide3" /></div></li>
</ul>
<div style="height:50px;">&nbsp;</div>
</div>

</div>
<!-- Main jumbotron for a primary marketing message or call to action -->

<!--<div id="banner-alatt">-->
<div class="container">
<!-- Example row of columns -->
<div class="row">
<div class="col-md-4">
<h2>Heading</h2>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Heading</h2>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Heading</h2>
<p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p>
<p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
</div>
</div>

<hr>

<footer>
<p>&copy; 2015 Company, Inc.</p>
</footer>
</div> <!-- /container -->
<!-- </div> -->

<!-- <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script> -->
<script src="js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>




Answer

The solution is: to wrap the images in a parent class and ad a child class to the images,

<span class="parent"><img class="child"...>

and to give them the centering, like this:

.parent {
    position: relative;
    overflow: hidden;
}

.child {
    position: absolute;
    left: -9999px;
    right: -9999px;
    margin: auto;
}

Then to ad an overflow:hidden; to the .banner ul li in order for the divs not to overlap each other.

Comments