【发布时间】:2014-05-02 20:21:22
【问题描述】:
我正在尝试在 div 更改时更改包含图像的文件夹。我正在使用媒体查询来更改页面加载时的 div。然后我需要 jquery 来检查 div 设置的大小,然后相应地更改图像文件夹。因此,例如,如果我的 div 的最大宽度为 700px,我希望 jquery 将字符串从摄影/移动/更改为摄影/全屏/原始字符串是摄影/全屏/stage.jpg,但我不希望整个字符串改变,只有词全屏、手机或平板电脑。我对 css 没问题,但我的 jquery 很糟糕,因为我目前正在学习!
这是代码,但 jquery 无法正常工作,因为媒体查询时图像不会改变:
<!DOCTYPE html>
<html>
<head>
<title>Change Image!</title>
<style type="text/css">
#surround{position: relative; margin: 0px auto; width: 100%; max-width: 700px; height: 467px; border: 1px solid black;}
@media screen and (max-width:800px){ #surround{max-width: 400px; height: 262px; border: 1px solid blue;} }
@media screen and (max-width: 500px){ #surround{max-width: 200px; height: 133px; border: 1px solid red;} }
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/init.js"></script>
</head>
<body>
<div id="surround">
<image src="photography/fullscreen/stage.jpg" class="picture" />
</div>
</body>
</html>
$(document).ready(function(){
if($("#surround").css("max-width") == "700px"){
$("img").each(function(){
$(this).attr("src", $(this).attr("src")("photography/fullscreen/"));
});
}
if($("#surround").css("max-width") == "400px"){
$("img").each(function(){
$(this).attr("src", $(this).attr("src")("photography/tablet/"));
});
}
if($("#surround").css("max-width") == "200px"){
$("img").each(function(){
$(this).attr("src", $(this).attr("src")("photography/mobile/"));
});
}
});
【问题讨论】:
-
这条线在做什么 $(this).attr("src", $(this).attr("src")("photography/tablet/")); ,你也可以分享一下你是从哪里得到这个语法的吗?
-
@gnanz 老实说,我不记得我从哪里得到语法了!我已经搜索了一个解决方案并从这里和那里复制了代码。然后我把它放在一起尝试让它与我有限的 jquery 知识一起工作。我想做的是:fourfront.us/blog/jquery-window-width-and-media-queries
-
但是!问题是我只想更改实际文件夹,因为该站点是一个摄影站点,并且在每个页面上的每个图像上更改字符串中的一行要容易得多!如果上面的代码可以工作,我认为这将是最好的解决方案?
-
在 jquery 中尝试“匹配”和“替换”方法。
-
我想这是我从这里获得代码的地方:stackoverflow.com/questions/16787306/…
标签: jquery css html media-queries