2013-02-12 197 views
28

我發現下面的在多個地方的代碼滑動左/右:幻燈片DIV左/右使用jQuery

$('#hello').hide('slide', {direction: 'left'}, 1000); 

但是,我不能得到它的工作。這是我嘗試的簡約測試:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us"> 
<head> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script> 
    $(document).ready(function() { 
     $("#test").click(function() { 
      $('#hello').hide('slide', {direction: 'left'}, 1000); 
     }); 
    }); 
    </script> 
</head> 
<body> 
    <article > 
     <div id="hello"> 
      Hello  
     </div> 
     <p><span id="test">Test</span> 
    </arcticle> 
</body> 

我在Chrome和Safari中試過了,它不起作用。

什麼問題?還有其他工作方法可以向左/向右滑動嗎?

+4

[這是因爲幻燈片是jQuery UI效果的一部分。](http://jqueryui.com/effect/) – Ohgodwhy 2013-02-12 00:07:16

回答

52

$('#hello').hide('slide', {direction: 'left'}, 1000);需要jQuery-ui庫。見http://www.jqueryui.com

+2

是的,你需要jqueryUI。看到的例子:http://jsfiddle.net/9wLGY/ – Jeff 2013-02-12 00:14:35

+1

JSFiddle https://jsfiddle.net/ZQTFq/3372/ – iYazee6 2015-10-07 12:21:33

60

您可以輕鬆獲得這種效果,而無需使用jQueryUI的,例如:

$(document).ready(function(){ 
    $('#slide').click(function(){ 
    var hidden = $('.hidden'); 
    if (hidden.hasClass('visible')){ 
     hidden.animate({"left":"-1000px"}, "slow").removeClass('visible'); 
    } else { 
     hidden.animate({"left":"0px"}, "slow").addClass('visible'); 
    } 
    }); 
}); 

試試這個工作小提琴:

http://jsfiddle.net/ZQTFq/

+4

我不認爲這是最乾淨的方式做到這一點,但工作像一個魅力。但是,我需要設置overflow-x:hidden;' – 2013-07-10 12:54:22

1

這樣做最簡單的方法是使用jQueryanimate.css動畫庫。

的Javascript

/* --- Show DIV --- */ 
$('.example').removeClass('fadeOutRight').show().addClass('fadeInRight'); 

/* --- Hide DIV --- */ 
$('.example').removeClass('fadeInRight').addClass('fadeOutRight'); 


HTML

<div class="example">Some text over here.</div> 


很容易的實現。只是不要忘了,包括在標題中animate.css文件:

0

$(document).ready(function(){ 
 
$("#left").on('click', function (e) { 
 
     e.stopPropagation(); 
 
     e.preventDefault(); 
 
     $('#left').hide("slide", { direction: "left" }, 500, function() { 
 
      $('#right').show("slide", { direction: "right" }, 500); 
 
     }); 
 
    }); 
 
    $("#right").on('click', function (e) { 
 
     e.stopPropagation(); 
 
     e.preventDefault(); 
 
     $('#right').hide("slide", { direction: "right" }, 500, function() { 
 
      $('#left').show("slide", { direction: "left" }, 500); 
 
     }); 
 
    }); 
 

 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 

 
<div style="height:100%;width:100%;background:cyan" id="left"> 
 
<h1>Hello im going left to hide and will comeback from left to show</h1> 
 
</div> 
 
<div style="height:100%;width:100%;background:blue;display:none" id="right"> 
 
<h1>Hello im coming from right to sho and will go back to right to hide</h1> 
 
</div>

$("#btnOpenEditing").off('click'); 
$("#btnOpenEditing").on('click', function (e) { 
    e.stopPropagation(); 
    e.preventDefault(); 
    $('#mappingModel').hide("slide", { direction: "right" }, 500, function() { 
     $('#fsEditWindow').show("slide", { direction: "left" }, 500); 
    }); 
}); 

它會像魅力看看演示。