2011-04-05 53 views
0

好吧,我有這Page,你可以看到,如果你向下滾動地圖,你會看到「巴里諾斯市場」的標誌和標題......如果你點擊閱讀更多鏈接文本擴展..這是正是我想要的。我試圖解決的問題是,當用戶點擊閱讀更多我需要在左邊的巴羅納市場圖像向下滾動文本,並下半部分....這裏是代碼...如何將圖像移動到div的一半?

<div id="barino_info"> 
<div id="barino_header"></div> 
<div class="info_body"> 
<div id="barino_left_body"> 
</div> 
<div class="right_body"> 
    <div class="right_body_outer"> 
     <ul class="play_navigation"> 
      <li class="active"><a href="#" class="navlinks barino_story_bottom" ref="right_body_inner">The Story</a></li> 
      <li><a href="#barino_video" class="navlinks barino_video_bottom" ref="bottom_barino_video">The Video</a></li> 
      <li><a onclick="show_gallery('barino');" href="#" class="navlinks barino_gallery_bottom" ref="bottom_barino_gallery">The Gallery</a></li> 
      <li><a href="#" class="navlinks barino_equipment_bottom" ref="barino_equipment">The Equipment</a></li>       
     </ul> 
    <div class="right_body_inner tab_content"> 
     Barino's Market is an ...... 

的圖像是在#barino_left_body背景網址

如何實現this..i想的是在策略的任何想法也許jQuery的偏移或類似的東西

我用這plugin到做擴大...這裏是我的jquery去做

$('.right_body_inner, .doughboy_right_body_inner, .haw_right_body_inner, .river_right_body_inner, .vib_right_body_inner, .barino_equipment, .dough_equipment, .river_equipment, .haw_equipment, .vib_equipment').expander({ 
    slicePoint:  355, // default is 100 
    expandEffect:  'fadeIn', 
    expandSpeed:  '8',  // speed in milliseconds of the animation effect for expanding the text 
    userCollapseText: '(less..)' // default is '[collapse expanded text]' 
}); 
+0

你在「Read More」上使用什麼代碼來展開div? – Khez 2011-04-05 02:05:46

+0

我更新了我的問題以回答您的問題 – Trace 2011-04-05 02:09:13

+0

只有在展開時才需要垂直居中的背景圖像? – Khez 2011-04-05 02:09:50

回答

1

後的「更多」點擊,就可以計算出周圍<div>的高度,並添加(或動畫)的#barino_left_body上邊距到周圍<div>高度的一半加上#barino_left_body高度的一半。

$('.read-more').click(function(event){ 
    var parentHeight = $(this).parents('.info_body').height(); 
    var imageHeight = $('#barino_left_body').height(); 
    var centerImage = (parentHeight-imageHeight)/2; 

    $('#barino_left_body').animate({"marginTop": centerImage}, 250); 

    event.preventDefault(); 
}); 

P.S.我很好奇你爲什麼將它作爲背景圖片。

+0

我不是硬背景圖像..如果你有更好的建議,更好的html佈局,我可以改變它 – Trace 2011-04-05 02:38:23

+0

它不影響你問的具體問題,但我認爲一個圖像比背景圖像更有意義在這種情況下,因爲你實際上想要顯示圖像。如果圖像與內容無關,我傾向於使用背景圖像。在這種情況下,徽標是內容的一部分。只是我對web開發的看法。 – 2011-04-05 03:06:15

-1

我不確定您是否可以使用jQuery移動背景圖片。我認爲你不能。但是你可以移動一個正常的圖像,所以這就是我會做的。

我會通過jquery選擇圖片,然後使用animate()來設置它的新位置。 css()也可以工作。頂部將是((股利的總大小) - (圖像的大小))/ 2

var h1 = $("#barino_left_body").height(); 
var h2 = $("#myimage").height(); 
var position = (h1-h2)/2; 
$("#myimage").animate({"top":position},250); 

您展開文本時做到這一點。

這應該可以做到。但是如果你真的需要使用圖像作爲背景,我不知道你的答案。 :(

我希望它幫助。

+0

你可以移動一個背景圖像與jQuery,雖然你的解決方案可能工作在任何其他情況下,他在他的要求中有點特別。 – Khez 2011-04-05 02:20:46

+0

你的回答就像我在完成我的工作。但是,無論如何,您可以爲背景圖片製作動畫,但您需要[Background-Position插件](http://snook.ca/archives/javascript/jquery-bg-image-animations/)。 – 2011-04-05 02:23:29

+0

@Khez你的意思是 – 2011-04-05 02:29:23

1

試穿尺寸爲這個代碼,我可能會得到一些錯誤引用與父/預防和控制,如果你不能弄清楚,發佈錯​​誤。

JS

$('.right_body_inner, .doughboy_right_body_inner, .haw_right_body_inner, .river_right_body_inner, .vib_right_body_inner, .barino_equipment, .dough_equipment, .river_equipment, .haw_equipment, .vib_equipment').expander({ 
    slicePoint:  355, // default is 100 
    expandEffect:  'fadeIn', 
    expandSpeed:  '8',  // speed in milliseconds of the animation effect for expanding the text 
    userCollapseText: '(less..)', // default is '[collapse expanded text]', 
    afterExpand: function($thisElement) { 
     $thisElement.parent().prev().height($thisElement.height()) 
    }, 
    onCollapse: function($thisElement, byUser) { 
     $thisElement.parent().prev().css('height',''); 
    } 
}); 

我添加2個回調基本上。

第一個afterExpand設置高度到展開div的高度。您已經正確設置圖像的位置的CSS。

第二個onCollapse集刪除我以前的高度定義,返回到div的CSS定義,並使佈局恢復到它的原始設計。

在第二個函數中使用.css(),所以您可以看到設置高度的不同方法,您可以在兩者之間進行選擇。

+0

也許有一個語法錯誤或什麼,因爲當我添加您的代碼擴展器停止工作,...什麼是$ thisElement – Trace 2011-04-05 02:43:39

+0

從[插件api文檔](http://plugins.learningjquery.com/ expander/index.html#options)... Hmz發現錯誤。固定! – Khez 2011-04-05 02:45:52