2013-03-19 51 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'); 
} 

真的沒人了嗎?