【发布时间】:2014-07-13 00:38:53
【问题描述】:
我有一个这样的 JavaScript
$(document).on('click', '.transparent-btns_nav', function(event) {
var images = $('.rslides ').find('img');
setTimeout(function() {
images.each(function() { // jQuery each loops over a jQuery obj
var h = $(this).outerHeight(true); // $(this) is the current image
var w = $(this).outerWidth(true);
// alert('source:'+$(this).attr('src'));
// alert('alto: '+h);
if (h < 290) {
$(this).addClass('small');
var m = 290 - h;
m = m / 2;
// alert('less height');
$('.small').attr('style', 'margin-top:' + m);
// $('.small').css('margin-top', +m + "px");
// $('.small').css('margin-bottom', +m + "px");
}
if (h > 290) {
$(this).addClass('big');
var m = h - 290;
m = m / 2;
// alert('less height');
$('.small').attr('style', 'margin-top:' - m);
}
});
}, 1000);
});
还有类似这样的媒体查询
@media only screen
and (min-device-width: 420px)
and (orientation: landscape) {
#bg {
}
#bg img {
margin - top: 0px;
}
.class {
margin-top: 0px;
margin-bottom: 0px;
}
.big {
margin-top: 0px;
margin-bottom: 0px;
}
}
我想要做的是使用 JavaScript 为类 big 和 small 添加属性,并在模式为 Landscape 时使用 MediaQuery 删除属性。但是我做不到,请有人帮我解决这个问题
更多解释
我正在尝试为大于或小于 div 的图像添加填充。就像它的高度小于顶部和底部的相等填充一样。如果图像的高度太大,那么我试图在顶部添加 -ve 填充并使图像居中。所有这些都是在纵向模式下动态完成的。如果手机变成横向,所有的内边距都应该去掉
【问题讨论】:
-
查找jquery css函数
-
你的意思是像写在css文件里?
-
如果你想检测orientationchange,你需要做一些阅读。 api.jquerymobile.com/orientationchange
-
no css 不会帮助他们在 style="" 中添加属性,例如 inline,我们无法使用 Media Queries 更改内联属性
-
当您可以定义新类并使用它们时,这似乎是一大堆麻烦。
标签: javascript jquery css media-queries