我想将文字放在图像上,所以我使用CSS来做到这一点。但是因为我使用Bootstap来制作它并且我觉得在CSS文件中放置绝对位置时我在调整大小时遇到了问题(文本没有居中),所以我制作了一个JQuery脚本来处理宽度变化,但它并没有&# 39;工作
WidthChangeScript.js
if($(window).width() > 500){
$( "#FactionsWidthtoPos" ).css( "top","255px" );
}
的index.html
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GrassCraft</title>
<!----Bootstrap---->
<link href="CSS/bootstrap.min.css" rel="stylesheet">
<link href="CSS/textoverimage.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<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="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#">Forums</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Shop<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Factions Grass</a></li>
<li><a id="FctQu" href="#" >Factions Quartz</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">How it works</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"></a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div>
</nav>
<!---Site body--->
<div id="imgTop" class="imageBehindText">
<img src="http://placehold.it/1920x800" class="img-responsive" width="1980" height="800">
<h2 id="FactionsWidthtoPos" class="textOverImage" >Factions</h2>
</div>
<div class="placeHolderImage">
<img src="http://placehold.it/1920x100" class="img-responsive" width="1980" height="800">>
</div>
<div class="container">
<div class="jumbotron">
<h1 id="abtUs" > GrassCraft</h1>
<p id="abtUsTxt" >A new Factions server made by 2 young enthusiasts. Any financial support would mean alot</p>
</div>
</div>
<!----Botstrap required JQuery---->
<script src="jquery.min.js"></script>
<script src="JavaScript/bootstrap.min.js"></script>
<script src="JavaScript/WidthChangeScript.js"></script>
</body>
textoverimage.css
.imageBehindText {
position: relative;
width: 100%; /* for IE 6 */
}
.textOverImage {
position: absolute;
top: 100px;
text-align: center;
width: 100%;
color: #f0f8ff;
text-decoration: underline;
}
.placeHolderImage {
opacity: 0;
}
这几乎是本网站的所有代码 如果你知道我可以优化代码的任何方式,请说
答案 0 :(得分:1)
您可以将代码置于jquery的resize事件处理程序中,如此
$(document).ready(function(){
function resize(){
if($(window).width() > 500){
if(!$( "#FactionsWidthtoPos" ).hasClass("something"))
$( "#FactionsWidthtoPos" ).addClass("something");
}else{
if($( "#FactionsWidthtoPos" ).hasClass("something"))
$( "#FactionsWidthtoPos" ).removeClass("something");
}
}
resize();
$(window).resize(function(){
resize();
});
});
你的css文件将包含一个新的类,如
.something{
top : 255px;
}
您还可以使用媒体查询来更新您的css窗口大小更改,如此
@media only screen and (min-width: 500px) {
.textOverImage {
top : 255px;
}
}
当您的窗口大小为最小500px
时,此媒体查询会自动更新您的.textOverImage类