javascript - 如何为幻灯片放映添加过渡?

标签 javascript html css css-transitions

我想为我正在为我的一个页面制作的幻灯片添加过渡效果。

这是代码,

        var index= 1;
        
        var toggle=0;
        
        function plusIndex(n){
        	index = index + n;
        	showImage(n);
        }
        showImage(1);
        
        function showImage(n){
        var i;
        var x = document.getElementsByClassName("placestovisit");
        if (index > x.length) {index=1};
        if (index < 1) {index= x.length};
        for (i=0; i<x.length; i++)
        {
          x[i].style.display="none";
        }
            x[index-1].style.display= "block";
        }
        
        autoSlide();   
        function autoSlide(){
        if (toggle) {return};
        var i;
        var x = document.getElementsByClassName("placestovisit");
        for (i=0; i<x.length; i++)
        {
          x[i].style.display="none";
        }
        if (index > x.length) {index = 1};
            x[index-1].style.display= "block";
        index++;
        setTimeout(autoSlide, 4000);
        }
    <body style="height:100%;">


<div id="placescontainer">
<div class="placestovisit fade">
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d2/Hanumangundi_Falls.jpg/1200px-Hanumangundi_Falls.jpg" alt="" />
      <h1>Hanumanagundi Falls</h1>
      <h2 > Suthanabbe Falls or Hanumanagundi Falls is located in the hilly surroundings of the Kudremukh National Park in the Chikkamagaluru district of Karnataka, India.
Suthanabbe Falls is located between Karkala and Lakya dam in the Kudremukh national park. The water falls from a height of 22 meters creating a scenic surroundings around it.</h2>
</div>

<div class="placestovisit fade">
      <img src="https://upload.wikimedia.org/wikipedia/commons/4/4b/Hampi_virupaksha_temple.jpg" alt="" />
      <h1> Hambi(Hambe) </h1>
          <h2> Hampi (Hampe) is a village and temple town recognised as a UNESCO World Heritage Site, listed as the Group of Monuments at Hampi. in northern Karnataka, India. It was one of the richest and largest cities in the world during its prime. It is located within the ruins of the city of Vijayanagara, the former capital of the Vijayanagara Empire. Predating the city of Vijayanagara, Hampi continues to be an important religious centre, housing the Virupaksha Temple and several other monuments belonging to the old city. According to statistics of 2014, Hampi is the most searched historical place in Karnataka on Google.</h2>
</div>
<div class="placestovisit w3-animate-fading">
      <img src="http://www.thebetterindia.com/wp-content/uploads/2012/09/IMG_0750-Com-800x533.jpg" alt="" />
      <h1> Agumbe </h1>
          <h2> Agumbe is a hill station which can be reached by bikes or cars. The way to this place is a diversion from Parkala (The diversion is just a few kms from DT). The best part of the trip to Agumbe is the ride. Beautiful sceneries on both sides will keep company all along the way. This place is abount 45 km from Manipal. Agumbe is one of the highest peaks of the Western Ghats located about 60kms from Udupi. Situated at a height of 830 metres from sea level, it overlooks the Arabian Sea. Agumbe is part of the Shimoga district which is a neighboring district of Udupi. </h2>
</div>

<div class="placestovisit fade">
      <img src="https://d28dwf34zswvrl.cloudfront.net/wp-content/uploads/2017/01/kerala-carbon-neutral-panchayat.jpg" alt="" />
      <h1> Kerala </h1>
          <h2> The border of Kerala is just 75 km from Manipal. The 300-year-old Bekal Fort, shaped like a giant key-hole, is one of the largest and best-preserved forts in Kerala. Surrounded by a splendid beach, the historic Bekal Fort offers a superb view of the Arabian Sea from its tall observation towers, where a few centuries ago huge cannons used to be placed. Today, it is one of the favourite spots for film-makers because the Bekal Fort and its surroundings with backwaters and hilly destinations make fascinating locales. This fort was made by Tipu Sultan and is built right next to the sea. The popular song 'Tu Hi Re' was picturised with this fort in the backdrop.</h2>
</div>

<div class="placestovisit fade">
      <img src="http://udupitoday.com/udtoday/images/uploads/January/images/jan1431attur1.jpg" alt="" />
      <h1> Attur church </h1>
          <h2> Attur Church situated in the outskirts of Karkala town in Karnataka state — India. It is situated 58 km from Mangalore.
Placed amidst placid greenery, the Attur-Karkala parish has a rich history with its origin tracing back to 1759. Moreover it is known for its miraculous history. Miracle, history, beauty, social activities, all bundled into one.
Unlike others, the church is a place of worship and belief for all, irrespective of caste and creed. An vidence for universal peace and brotherhood. People from all walks of the society come here to offer their prayers to St Lawrence</h2>
</div>
<button class="btn" onclick="plusIndex(-1)" id="btn1"> &#10094;</button>
<button class="btn"  onclick="plusIndex(1)" id="btn2"> &#10095;</button>

<button class="play"  onclick="toggle=1-toggle; autoSlide()" id="btn3">  Play/Pause </button>

</div>

    </body>

Fiddle Demo

我是 javascript 和 css 的新手。

以上工作正常,离线过渡,我不知道 fiddle 有什么问题。我认为这有一些小的误解..

谁能帮我添加一个幻灯片过渡。

我需要类似链接 http://www.devtrix.net/sliderman/examples.html 的东西

最佳答案

问题是您将 css 显示属性交换为 display: none,这会立即使 div 不可见,而不是等待任何淡入淡出。

与其立即打开/关闭显示,不如尝试淡出:

fadeOut("slow", function () {
$(this).css({display:"none"});
});

区别在于fadeOut的第二个参数是一个回调函数,淡出完成后运行。

关于javascript - 如何为幻灯片放映添加过渡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44633866/

相关文章:

html - 如何在父节点后面放置伪元素?

javascript - 当打开的弹出窗口的位置发生变化时得到通知

javascript - Highcharts 最小 y 轴值

javascript - 如何检测 Chrome 中的弹出窗口拦截器?

html - 如何使用 Bootstrap 将图像放在图像上

jquery - 使用 Jquery 将项目插入给定索引处的无序列表

javascript - 无法使用 jQuery 获得正确的类值

javascript - 如果在 Protractor js 中找到相同的类列表,则按文本选择下拉选项

html - 自定义列宽表bootstrap cshtml可滚动

javascript - 粒子地面.js : how to make website scrollable