2013-03-19 101 views
2

如何让我的页面上的元素,以重置后的字体增加违约/减少获取元素重置为默认大小后字体增大/减小

我试过以下,但收效甚微(字体增加/减少工作):

jsfiddle

这里是我的代码:

$(".resetFont").click(function() { 
}); 

$(".increaseFont").click(function() { 
    var fontSize = getFontSize(); 
    var newFontSize = fontSize + 1; 
    setFontSize(newFontSize); 
    return false; 
}); 

$(".decreaseFont").click(function() { 
    var fontSize = getFontSize(); 
    var newFontSize = fontSize - 1; 
    setFontSize(newFontSize); 
    return false; 
}); 

function getFontSize() { 
    var currentSize = $("html").css("font-size"); 
    var currentSizeNumber = parseFloat(currentSize, 12); 
    if (currentSizeNumber > 24) { 
     currentSizeNumber = 24; 
    } 
    if (currentSizeNumber < 10) { 
     currentSizeNumber = 10; 
    } 
    return currentSizeNumber; 
} 

function setFontSize(size) { 
    $("html").css("font-size", size); 
    $(".actualSize").html(size); 
} 
+0

'12'应该在'parseFloat(currentSize,12);'中? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat – 2015-12-22 21:39:34

回答

3
var defaultFontSize = $('html').css('font-size'); 

$(".resetFont").click(function() { 
    $('html').css('font-size', defaultFontSize); 
}); 

Updated DEMO

0
var defaultFontSize = $('html').css('font-size'); 
$(".resetFont").click(function() { 

$("html").css("font-size", defaultFontSize); 
    $(".actualSize").html(defaultFontSize); 


}); 
+0

什么是'size'?以及为什么你将html内容设置为'.actualSize' – gdoron 2013-03-19 15:11:58

+0

@gdoron这是在OP的文章中,似乎是一个元素,表示字体大小,并且需要在其更改时更新。 – Bill 2013-03-19 15:16:48

0

只需设置你想要的原始字体大小是什么:

$(".resetFont").click(function() { 
     $("html").css("font-size", '24px'); 
}); 
0
$('.resetFont').click(function(){ 
    setFontSize('auto'); 
} 

真的没人了吗?