Javascript 如何为不同的屏幕大小编写响应文本
如果我想制作响应性文本,我可能会这样写:Javascript 如何为不同的屏幕大小编写响应文本,javascript,jquery,responsive-design,Javascript,Jquery,Responsive Design,如果我想制作响应性文本,我可能会这样写: $(window).resize(function () { if $(document).width() <= 320) { $('#mydiv').html('something') } if $((document).width() > 320) && (document).width() <= 480)) { $('#mydiv').html('some
$(window).resize(function ()
{
if $(document).width() <= 320) {
$('#mydiv').html('something')
}
if $((document).width() > 320) && (document).width() <= 480)) {
$('#mydiv').html('something longer')
}
if $((document).width() > 480) && (document).width() <= 768)) {
}
//and so on
}
在我这样做之后,有没有一个简单的函数可以用来获取这些数据并简化我的多条件函数?告诉javascript如何构建我需要的特定函数的某种方式
类似这样的东西,但在实际代码中:
for (objects in divTextPerScreenWidthMap) {
create If Statement for Nth Object(object)
}
谢谢,
另外我认为使用这样的查找表有点过头了。请注意,如果正确使用
else,代码会变得简单得多
if $(document).width() <= 320) {
$('#mydiv').html('something')
} else if ($(document).width() <= 480) {
$('#mydiv').html('something longer')
} else if ($(document).width() <= 768) {
...
}
if$(document).width()类似的方法应该可以工作-它将为第一个屏幕宽度(360)设置一次,然后为后续的每个屏幕宽度设置一次,如果宽度小于文档的宽度,它将覆盖它
var docWidth = $(document).width();
for (var i in divTextPerScreenWidthMap)
{
if (parseInt(divTextPerScreenWidthMap[i][0]) < docWidth || i == 0)
{
// Set something here
console.log( divTextPerScreenWidthMap[i][1] );
}
}
var docWidth=$(document).width();
for(divTextPerScreenWidthMap中的变量i)
{
if(parseInt(divTextPerScreenWidthMap[i][0])
这是一个缩短我没有想到的代码的好方法——但我正在寻找能够获取对象的东西——因此我将有多个对象,每个对象都能够产生不同的结果
var docWidth = $(document).width();
for (var i in divTextPerScreenWidthMap)
{
if (parseInt(divTextPerScreenWidthMap[i][0]) < docWidth || i == 0)
{
// Set something here
console.log( divTextPerScreenWidthMap[i][1] );
}
}