跳到主要內容

jQuery - 如何用Slider控制字型大小

上一篇文章是用三個按鈕控制字型大小,感覺不太靈活,就到jQuery的官方網站,參考了這個 UI Slider - Snap to increments 修改,沒想到很順利的修改完成,以下是我做的Demo,真是感謝有jQuery出現!! 讓網頁世界更加美好。

1.先嵌入jQuery 涵式庫
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>


2.加入jQuery 程式
<script>

$(document).ready(function(){
var $speech = $('div.speech');
var defaultSize = $speech.css('fontSize'); -->預設的字形大小12px
var num= parseFloat(defaultSize, 10); -->只取12px的數字部份,並轉換成數字

$("#slider").slider({
value:num,
min: 8,-->字型大小最小值
max: 20, -->字型大小最大值

step: 1,
slide: function(event, ui) {
$("#amount").val( ui.value + 'px');
$speech.css('fontSize' , ui.value + 'px');
}
});
$("#amount").val($("#slider").slider("value") +'px');
});
</script>

3.編寫適當的css
<style type="text/css">
/*base*/
body {
font:normal 12px Verdana, Geneva, sans-serif;
}

/*end base*/

/*slider*/
.ui-slider { position: relative; text-align: left; }
.ui-slider .ui-slider-handle { position: absolute; z-index: 2; width: 1.2em; height: 1.2em; cursor: default; }
.ui-slider .ui-slider-range { position: absolute; z-index: 1; font-size: .7em; display: block; border: 0;}
.ui-slider-horizontal { width:100px; height: 11px; background:url(images/slider_rg_100.gif) 0 0 no-repeat; }
.ui-slider-horizontal .ui-slider-handle { top: -1px; height:13px;margin-left: -.6em; background:black; background:url(images/slider_hd_1.gif) 0 0 no-repeat;}
.ui-slider-horizontal .ui-slider-handle:hover{background:url(images/slider_hd_1_hover.gif) 0 0 no-repeat;}
.ui-slider-horizontal .ui-slider-range { top: 0; height: 100%; }
.ui-slider-horizontal .ui-slider-range-min { left: 0; }
.ui-slider-horizontal .ui-slider-range-max { right: 0; }
/*end slider*/

</style>

4.大功告成!
我的Demo

留言

這個網誌中的熱門文章

Indexed Database API - Initail

As a front web developer, I could create UI faster or Optimize file to load page faster. But it hard to faster response time form server however 80-90% of the end-user response time is spent on the frontend. Nowadays we could use the Indexed Database API, or IndexedDB to reduce the request times. Less request times, less response time. What is IndexedDB ? IndexedDB is web browser standard interface for a local database of records holding simple values and hierarchical objects. IndexedDB was initially proposed by Oracle in 2009./* wiki */ What browsers would support IndexedDB ? Most browsers of hybrid devices support it exclude IE9-, Safari 7-,iOS Safari 7.1-, Opera Mini, Android Browser 4.3-./* caniuse.com */ When shall we use IndexedDB ? Save Money on web hosting Data index sort Data keyword search Keep data for period of time How to start it? //*************************************************************** //initDB //{Object}, {data: dbName, objectStore: storeName, ...

最近設計的一組icon..太有APPLE風格被打槍了

jQuery 的slideToggle太好用了

$ ( ".collapse" ) .each ( function ( index, value ) { $ ( this ) . click ( function ( evt ) { evt.preventDefault ( ) ; //prevent the default click event of the anchor tag. var target = $ ( this ) .attr ( "data-toggle" ) ; $ ( "." + target ) .slideToggle ( ) ; } ) ; } ) ; http://code-tricks.com/create-a-common-toggler-for-div-using-jquery/