2015-10-18 68 views
-1

我有一個關於鼠標滑過時顯示圖片的問題,聲音(非常短,就像一個bing)會播放。在正常情況下,我知道如何使用html,css和javascript來做到這一點。我在這裏有一個很好的例子:https://freesound.org/people/Timbre/sounds/232210/ 並且讓我們說,Drupal中的類page.tpl.php被稱爲圖片。由於我是Drupal的新手,我應該如何通過僅使用javascript或jquery使這個函數工作?任何意見?Drupal在鼠標上播放聲音

回答

0

對於Drupal,你應該添加你的腳本drupal_add_js()。您可以添加文件或內聯js,並設置多個選項,例如它應該出現在每個頁面上,還是應該添加到頁腳或頁眉以及是否需要jQuery。

你可以創建一個像這裏面一個js文件:

jQuery(document).ready(function($) { 
    var mySound = document.createElement('audio'); 
    mySound.setAttribute('src', '/path/to/my/sound.mp3'); 

    //play the sound when the visitor moves the mouse over 
    //an element with the class 'picture'. 
    $('.picture').mouseover(function() { 
     mySound.play(); 
    }); 
}); 

然後在你的主題或模塊,你可以使用hook_page_alter添加您的JS:

drupal_add_js(
    '/path/to/js/script.js', 
    array(
    'type' => 'file', 
    'scope' => 'footer', 
    'group' => JS_THEME, //or JS_DEFAULT if module 
    'every_page' => false, 
    'requires_jquery' => true, 
) 
); 

你可以換行在某種if語句中只將它加載到你需要的頁面上。

希望有幫助!

+0

嗨!很多tks。這真的很有用!:-) – drupal