2015-07-21 55 views
1

按照this answer中列出的步驟,我將光標設置爲FontAwesome圖標。現在,我想將光標設置爲任何圖標,按類名稱(例如,fa-pencil)。 爲了實現這一點,我似乎需要能夠以編程方式查找給定圖標的unicode值。以編程方式獲得FontAwesome unicode值

我知道這些值列在font-awesome.css樣式表中,但我想避免解析該文件,如果存在其他方法。

這可能嗎?

+0

什麼輸入?如:你想要一個特定的圖標,但該圖標是如何選擇的? – FWDekker

+0

輸入是所需圖標的類名稱。例如,「鉛筆」。我會相應更新我的問題! –

+0

我認爲@Waflix所問的是,如果類名是動態的(可更改的),並且如果是的話,用戶將如何改變它?他們有一個文本框,他們鍵入類名或下拉選擇它? – GPicazo

回答

2

我已經kludged在一起,一些作品:

var setCursor = function (icon) { 
    var tempElement = document.createElement("i"); 
    tempElement.className = icon; 
    document.body.appendChild(tempElement); 
    var character = window.getComputedStyle(
     document.querySelector('.' + icon), ':before' 
    ).getPropertyValue('content'); 
    tempElement.remove(); 

    var canvas = document.createElement("canvas"); 
    canvas.width = 24; 
    canvas.height = 24; 
    var ctx = canvas.getContext("2d"); 
    ctx.fillStyle = "#000000"; 
    ctx.font = "24px FontAwesome"; 
    ctx.textAlign = "center"; 
    ctx.textBaseline = "middle"; 
    ctx.fillText(character, 12, 12); 
    var dataURL = canvas.toDataURL('image/png') 
    $('body').css('cursor', 'url('+dataURL+'), auto'); 
} 

這會用給定的類創建一個臨時元素,然後使用window.getComputedStyle來獲取:before僞元素的內容。

謝謝大家的一切幫助!

1

你可以做的是使用一個隱藏的div來放置圖標。一旦到位,讀取裏面的字符,獲取它的值並將其轉換爲unicode表示。完成之後,您可以在the code you gave中將其用作光標顯示。請注意,您必須使用getComputedStyle()才能獲取應用該圖標的CSS值。

你可以做到這一點,像這樣:

HTML

<div style="display:none;"><i id="fontTest"></i></div> 

JS

function onSubmit() { 
    var userValue = document.getElementById("#someElement").value; 
    var fontTest = document.getElementById("#fontTest"); 
    fontTest.className = fontTest.className + " " + userValue; 

    var style = window.getComputedStyle(fontTest); 
    var character = String.fromCharCode(style.getPropertyValue("contents")); 
    // The character value is now the unicode representation of the icon 
} 
+0

問題是元素將是空的; Font Awesome圖標純粹由CSS實現。 – JJJ

+0

@Juhana更新回答解決這個問題。 – FWDekker

+1

這不適合我。我相信它與FontAwesome使用的'before'僞元素有關。 –

4

可能晚了,但是這將允許你這樣做: elt.innerHTML = faUnicode('pencil');

也許它可以幫助別人尋找同樣的事情。

function faUnicode(name) {'use strict'; 
    // Create a holding element (they tend to use <i>, so let's do that) 
    const testI = document.createElement('i'); 
    // Create a realistic classname 
    // - maybe one day it will need both, so let's add them 
    testI.className = `fa fa-${name}`; 
    // We need to append it to the body for it to have 
    // its pseudo element created 
    document.body.appendChild(testI); 

    // Get the computed style 
    const char = window.getComputedStyle(
    testI, ':before' // Add the ':before' to get the pseudo element 
).content.replace(/'|"/g, ''); // content wraps things in quotes 
           // which we don't want 
    // Remove the test element 
    testI.remove(); 

    return char.charCodeAt(0); 
} 

或者在ECMA5:

function faUnicode(name) { 
    var testI = document.createElement('i'); 
    var char; 

    testI.className = 'fa fa-' + name; 
    document.body.appendChild(testI); 

    char = window.getComputedStyle(testI, ':before') 
      .content.replace(/'|"/g, ''); 

    testI.remove(); 

    return char.charCodeAt(0); 
} 
相關問題