2017-09-13 32 views
0

我對css瞭解不多,但我認爲這段代碼可以幫助我生成一個選取框。基本上我想要用盒子完成的動畫,用文本來完成。生成動畫就好像它是一個選取框

enter image description here 我的主要問題發生在動畫中,它不是很流暢,我希望它更流暢,它從容器的末端開始到左側。我該怎麼做?我會很感激。

http://jsfiddle.net/joof5dhx/

<div id="horizontalScroller"> 
<div>it is a message a little more of 100 characteres</div> 
<div>it is a message a little more of 110 characteres</div> 
<div>it is a message a little more of 120 characteres</div> 
<div>it is a message a little more of 130 characteres</div> 
</div> 

window.horizontalScroller = function($elem) { 
    var left = parseInt($elem.css("left")); 
    var temp = -1 * $('#horizontalScroller > div').height(); 
    if(left < temp) { 
     left = $('#horizontalScroller').height() 
     $elem.css("left", left); 
    } 
    $elem.animate({ left: (parseInt(left)-60) }, 900, function() { 
     window.horizontalScroller($(this)) 
    }); 
} 


$(document).ready(function() { 
    var i = 0; 
    $("#horizontalScroller > div").each(function() { 
      $(this).css("left", i); 
      i += 60; 
      window.horizontalScroller($(this)); 
    }); 
}); 

http://jsfiddle.net/hhcbtyyg/

+0

目前已經覆蓋字幕效果等問題。 [這一個](https://stackoverflow.com/questions/21233033/css3-marquee-effect)包括一些使用CSS沒有JS的技術。 – nnnnnn

+0

@nnnnnn這樣,選取框將是有限的。與此我保證有一個無限的選取框。 – yavg

回答

1

你只可以:

window.horizontalScroller = function($elem) 
{ 
    var left = parseInt($elem.css("left")); 

    $elem.animate({ left: (parseInt(left)-60) }, 900, function() 
    { 
     // get the current left of the element 
     var currentLeft = parseInt($(this).css("left")); 
     // get the width of the element 
     var width  = $(this).width(); 
     // get the container 
     var container = $(this).parent("#horizontalScroller"); 
     // get the width of the container 
     var containerWidth = $(container).width(); 

     // check if the element goes out of the view item X + item w < 0 
     if ((currentLeft + width) <= 0) 
     { 
     // put it on the opposite side: simply container w + item w 
     $(this).css("left", (containerWidth + width) + "px"); 
     } 

     window.horizontalScroller($(this)) 
    }); 
} 

我只是不明白,爲什麼你在上面代碼中使用height。如果有什麼我不知道的讓我知道。

更新:

爲了使項目在默認情況下在最左邊的出現:

$(document).ready(function() { 
    var container = $("#horizontalScroller"); 
    var children = $(container).children(); 
    var containerW = $(container).width(); 

    // Loop through each item of container 
    for (var i = 0; i < children.length; i++) 
    { 
     var item = children[i]; 
     var itemW = $(item).width(); 
     // this is simply the space between them, remove if you don't need it 
     var padding = 10 * (i + 1); 

     // simply: padding + Container Width + (Item Width * (i + 1)) 
     // (Item Width * (i + 1)) because you need to position each element beside each other. 
     $(item).css("left", (padding + containerW + itemW * (i + 1)) + "px"); 
     window.horizontalScroller($(item)); 
    } 
}); 

your updated fiddle

希望幫助

+0

謝謝,但你的代碼不適合我..你有所有的理由,而不是高度的寬度 – yavg

+0

我忘了把你更新的小提琴我做了http://jsfiddle.net/joof5dhx/2/ – masterpreenz

+0

你是一個真正的!謝謝。但我需要動畫從容器的右側開始(如圖所示)。這可以做出反應?而不取決於容器寬度的默認值... – yavg

0

嗨選中此版本的的jsfiddle的,我做了一些modificaitons,因爲你的動畫無論從任何高度開始的值是您的DIV了。檢查這個我試圖匹配你的css和寬度在你的css的高度,我只注意到你的js中的「左」var獲取你的元素的高度。

CSS:

#horizontalScroller { 
position: absolute; 
width:300px; 
height: 300px; 
border: 1px solid red; 
overflow: hidden; 
} 

也許你能得到一些提示如何完成它的響應方式。 JSFIDDLE

+0

謝謝!你幾乎可以做到!你怎麼能對容器的任何寬度做出響應? – yavg

+0

我試過這個.. http://jsfiddle.net/6gt0u3nb/ – yavg

+0

如果你想讓它響應(這是你的意思,它調整到寬度),以及你必須設置在100%的股利,爲div調整任何屏幕大小,並再次檢查這個jsfiddle,我已經改變了動畫所遵循的參數,所以不是高度,我們將遵循寬度設置。這裏http://jsfiddle.net/joof5dhx/3/ – jhek

相關問題