codice:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Left static - Top fixed</title>
<style type="text/css">
html,body{
height:150%;
width:150%;
}
/*left in pixel*/
/*#fixedY{
position:fixed;
top:100px;
left:50px;
width:300px;
height:200px;
background:red;
}*/
/*left percentuale (per centrare)*/
#fixedY{
position:fixed;
top:100px;
left:50%;
width:300px;
height:200px;
background:red;
}/*il margin-left negativo deve essere inline per essere leggibile da js*/
</style>
<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
var initialLeft;
$(function(){
initialLeft=Number($('#fixedY').offset().left)-parseInt($('#fixedY').css('marginLeft'));
});
$(window).scroll(function(){
$('#fixedY').css('left',initialLeft-$(window).scrollLeft());
});
</script>
</head>
<body>
<div id="fixedY" style="margin-left:-150px;"></div>
</body>
</html>
Questa soluzione è valida sia per per una "barra" allineata a sinistra (CSS commentato) che per una barra centrata (CSS non commentato).
Se usi una barra centrata, il margin-left negativo (pari a metà della larghezza, width, della barra) deve essere dichiarato inline, come nell'esempio, perchè sia leggibile da js.