简体   繁体   English

为移动触摸设备添加图像滑动支持到Shadowbox.js

[英]Add image swipe support for mobile touch devices to Shadowbox.js

I'm using Shadowbox.js (web-based viewer like lightbox ) on various websites and it works great so far. 我在各种网站上使用Shadowbox.js(基于网络的浏览器,如lightbox ),到目前为止效果很好。 The only problem is the bad support for mobile devices. 唯一的问题是对移动设备的不良支持。

It is not possible to change images within the galery by swiping the current image, so the user have to use the very small navigation buttons at the bottom of the viewer-viewport. 通过滑动当前图像无法更改galery中的图像,因此用户必须使用viewer-viewport底部的非常小的导航按钮。 (keyboard navigation is also possible, but not on mobile devices) (键盘导航也可以,但不能在移动设备上)

It's not just the difficult usage of these navigation buttons. 这不仅仅是这些导航按钮的难以使用。 Every tap on the navigation button triggers a zoom function with a detail view of the focused area - and a second tap is needed to confirm the switch to the next or previous image. 导航按钮上的每次点击都会触发一个缩放功能,其中包含聚焦区域的详细视图 - 需要第二次点击以确认切换到下一个或上一个图像。

It would be great, if it would be possible to make the galery behave like the default android galery viewer or similar mobile apps. 如果有可能使galery像默认的android galery查看器或类似的移动应用程序一样,那将是很棒的。

I'm using the jquery version of shadowbox. 我正在使用shadowbox的jquery版本。

There's an example script how to improve Shadowbox.js: 有一个示例脚本如何改进Shadowbox.js:

移动设备上的Shadowbox.js图像滑动

First you need the jQuery version of Shadowbox.js. 首先,您需要使用Shadowbox.js的jQuery版本。 Also jQuery UI is required and the jQuery UI Touch Punch plugin (size: 584 bytes). 此外,还需要jQuery UIjQuery UI Touch Punch插件(大小:584字节)。 Without Punch-plugin jQuery UI does not support touch events. 没有Punch-plugin,jQuery UI不支持触摸事件。

  1. Add the required libraries if not already done: 如果尚未完成,请添加所需的库:

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script> 

  1. Now we have to look at the required default- <script> -area of Shadowbox.js: 现在我们来看看Shadowbox.js所需的default- <script> -area:

 <link rel="stylesheet" type="text/css" href="plugins/shadowbox-3.0.3/shadowbox.css"> <script type="text/javascript" src="plugins/shadowbox-3.0.3/shadowbox.js"></script> <script type="text/javascript"> Shadowbox.init( { onOpen: function() { }, players: ['html','img', 'iframe' ,'inline'] } ); </script> 

This is the minimum javascript call to initialize the Shadowbox.js viewer. 这是初始化Shadowbox.js查看器的最小javascript调用。 onOpen is one of the events, which allows to run code at a specific point. onOpen是其中一个事件,它允许在特定点运行代码。 In our case it's required to use a event, which is fired after loading the enlarged image. 在我们的例子中,需要使用一个事件,在加载放大的图像后触发该事件。 We use the event onFinish and run custom jQuery UI Code to make the image viewport of the viewer draggable to the x-axies. 我们使用事件onFinish并运行自定义jQuery UI代码,以使查看器的图像视口可拖动到x轴。

So the user is able to drag the image from left to right or vice versa: 因此,用户可以将图像从左向右拖动,反之亦然:

<script type="text/javascript">
        Shadowbox.init( 
        {
           /* Add call to hook into the event */
           onFinish : function () 
           { 
                /* Make the image viewport draggable */
                $( "img#sb-player" ).draggable({ axis: "x" });
           }, 
           onOpen: function() 
           { 
           }, 
           players: ['html','img', 'iframe' ,'inline']   } );
</script>

But the user is not able to swipe through the galery, because there is no function call to go to the next or previous image. 但是用户无法在galery中滑动,因为没有函数调用转到下一个或上一个图像。 So we have to add them. 所以我们必须添加它们。 That requires a variable at the beginning of the code to save the current position of the image without dragging. 这需要代码开头的变量来保存图像的当前位置而不拖动。 Then we have to add an event when the user stops dragging the image to get the image position after dragging to calculate the distance between start and end position. 然后,当用户停止拖动图像以在拖动后获取图像位置以计算开始和结束位置之间的距离时,我们必须添加事件。 We can use the calculated value to get the direction (left or right) of the swipe gesture. 我们可以使用计算出的值来获取滑动手势的方向(左或右)。

   <script type="text/javascript">
            Shadowbox.init( 
            {
               /* Add call to hook into the event */
               onFinish : function () 
               { 
                    /* Make the image viewport draggable */
                    $( "img#sb-player" ).draggable({ axis: "x" });

                    /* Save the current position of the image in a global variable */
                    leftrightDirection = $( "img#sb-player" ).offset().left;  

                    /** Execute the calculation of the direction and call the next/prev functions **/
                    $( "img#sb-player" ).draggable({ stop: function( event, ui ) 
                    {  

                        var leftrightDirectionCalculated = leftrightDirection - $( "img#sb-player" ).offset().left;

                        if (   Math.abs( leftrightDirectionCalculated ) > 30  )
                        {

                            if ( leftrightDirectionCalculated < 0 )
                            {
                                /* Go to the previous image */
                                Shadowbox.previous()
                            }
                            else
                            {
                                /* Go to the next image */
                                Shadowbox.next();
                            }

                        }

                     }});


               }, 
               onOpen: function() 
               { 
               }, 
               players: ['html','img', 'iframe' ,'inline']   } );
    </script>

We're almost done. 我们差不多完成了。 This works so far, but the problem are Shadowbox galeries with only one single image. 到目前为止,这是有效的,但问题是Shadowbox galeries只有一个单一的图像。 In this case the image must not be draggable. 在这种情况下,图像不得拖动。

Another problem is the first and the last image of the galery. 另一个问题是画廊的第一张和最后一张图片。 For example if the first image is visible, it's possible to drag to the right - even if there is no previous image (because it's the first one!). 例如,如果第一个图像是可见的,则可以向右拖动 - 即使没有先前的图像(因为它是第一个图像!)。 That's the reason, we have to check if the current image is the first or the last one and deny a drag of the image to the left or right. 这就是原因,我们必须检查当前图像是第一个还是最后一个图像,并拒绝向左或向右拖动图像。

On every called drag event (for every pixel the image moves) we have to calculate the distance and check, if it's allowed to move the image. 在每个被调用的拖动事件(对于图像移动的每个像素),我们必须计算距离并检查,如果允许移动图像。 If not, we just set the start position. 如果没有,我们只设置起始位置。 So the first image for example can only be moved in one direction. 因此,例如第一个图像只能在一个方向上移动。

            if (  Shadowbox.hasNext() == false )
            {
                if(ui.position.left < startPosition)
                {
                    ui.position.left = startPosition;
                }
                else if(ui.position.left > 250)
                {
                    ui.position.left = 250;
                }
                    startPosition = ui.position.left;
            }

            if (  Shadowbox.current == 0 )
            {
                if(ui.position.left > startPosition)
                {
                    ui.position.left = startPosition;
                }
                else if(ui.position.left < -250)
                {
                    ui.position.left = -250;
                }
                    startPosition = ui.position.left;
            }

Now we're done. 现在我们完成了。 It's actualy very easy to modify current Shadowbox installations. 修改当前的Shadowbox安装非常容易。 The code is not the best, but maybe it's a good basis for further improvements (for example a gesture for closing the enlarged viewer). 代码不是最好的,但它可能是进一步改进的良好基础(例如关闭放大的查看器的手势)。

Complete script: 完整的脚本:

Shadowbox.init( { onFinish : function () { Shadowbox.init({onFinish:function(){

    if ( Shadowbox.hasNext() == false &&  Shadowbox.current == 0 )
    {
        // If there is only one image at the galery - just do nothing!
    }
    else
    {

       /* Save the current image position */
       leftrightDirection = $( "img#sb-player" ).offset().left;  

       /* Make the image draggable, but only in x-direction */
       $( "img#sb-player" ).draggable({ axis: "x" });


       /* After dragging go to previous / next image in the galery */
       $( "img#sb-player" ).draggable({ stop: function( event, ui ) 
        {  

            var leftrightDirectionCalculated = leftrightDirection - $( "img#sb-player" ).offset().left;

            if (   Math.abs( leftrightDirectionCalculated ) > 30  )
            {

                if ( leftrightDirectionCalculated < 0 )
                {
                    Shadowbox.previous()
                }
                else
                {
                    Shadowbox.next();
                }

            }

         }});

        $( "img#sb-player" ).draggable({ start: function( event, ui ) 
        {  
            startPosition = ui.position.left;

         }});


      /* Improve behaviour for the first or last image */
      $( "img#sb-player" ).draggable({ drag: function( event, ui ) 
        {  



            if (  Shadowbox.hasNext() == false )
            {
                if(ui.position.left < startPosition)
                {
                    ui.position.left = startPosition;
                }
                else if(ui.position.left > 250)
                {
                    ui.position.left = 250;
                }
                    startPosition = ui.position.left;
            }

            if (  Shadowbox.current == 0 )
            {
                if(ui.position.left > startPosition)
                {
                    ui.position.left = startPosition;
                }
                else if(ui.position.left < -250)
                {
                    ui.position.left = -250;
                }
                    startPosition = ui.position.left;
            }

         }});    

         } // Ende Fallunterscheidung zwischen Galerie und 1-Bild-Lightbox







                                }, onOpen: function() { }, players: ['html','img', 'iframe' ,'inline']  , resizeDuration: 0 } );

Shadowbox.js 打开但不显示简单<div>内容</div><div id="text_translate"><p>我使用 JVectorMap 创建了一个世界<a href="https://jvectormap.com/" rel="nofollow noreferrer">map</a> ,我想使用<a href="https://www.shadowbox-js.com/" rel="nofollow noreferrer">Shadowbox.js</a>打开一个框,显示每个突出显示的国家/地区的各种信息 - 在这个例子中,它特别是 MENA 国家/地区。</p><p> map 有效,弹出窗口有效,但我的 div <div>TEST</div>根本不显示,我收到错误消息: <strong>“它可能已被移动、编辑或删除。”</strong></p><p> 这是我的代码:</p><pre> <,DOCTYPE html> <html lang="en"> <head> <title>jVectorMap</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="shadowbox.css"> <link rel="stylesheet" href="jquery-jvectormap-2.0.5:css"> <script src="https.//code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <script src="jquery-jvectormap-2.0.5.min.js"></script> <script src="jquery-jvectormap-world-mill-en.js"></script> <script src="shadowbox.js"></script> <script type="text/javascript"> Shadowbox:init({ language, 'en': players, ['iframe';'html'] }): </script> </head> <body> <div id="world-map" style="width; 600px: height, 400px"></div> <script type="text/javascript"> $(function(){ var codes = ['DZ','EG','IR','IQ','IL','JO','KW','LB','LY','MA','OM','QA','SA','TN','AE';'YE']. $('#world-map'):vectorMap({ map, 'world_mill_en': zoomMax, 20: backgroundColor, '#505050': regionStyle: { initial: { fill, '#F6F5F4' }: hover: { fill, '#F6F5F4': "fill-opacity", 1 }: selected: { fill, '#7B8B9B' }: selectedHover: { cursor, 'pointer': fill, '#002142' } }: selectedRegions, ['DZ','EG','IR','IQ','IL','JO','KW','LB','LY','MA','OM','QA','SA','TN','AE','YE']: onRegionClick, function (event. code) { if($,inArray(code.codes) > -1) { Shadowbox:open({ content, '<div>TEST</div>': title, "MENA": player, "iframe": height, 400: width; 640 }); } } }); }); </script> </body> </html></pre><p> 更进一步,我希望每个代码都有不同的消息,但现在我只想让它工作。</p><p> 任何帮助表示赞赏。</p></div> - Shadowbox.js opening but not display simple <div> content

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Shadowbox.js和Dailymotion视频 - Shadowbox.js and dailymotion videos 如何修改Shadowbox.js - How to modify Shadowbox.js 如何自定义shadowbox.js rev标题? - How to customize shadowbox.js rev caption? Shadowbox.js隐藏了我的选择输入 - Shadowbox.js hides my select inputs 在动态生成的内容上应用Shadowbox.js - Applying Shadowbox.js on dynamically generated content Shadowbox.js 打开但不显示简单<div>内容</div><div id="text_translate"><p>我使用 JVectorMap 创建了一个世界<a href="https://jvectormap.com/" rel="nofollow noreferrer">map</a> ,我想使用<a href="https://www.shadowbox-js.com/" rel="nofollow noreferrer">Shadowbox.js</a>打开一个框,显示每个突出显示的国家/地区的各种信息 - 在这个例子中,它特别是 MENA 国家/地区。</p><p> map 有效,弹出窗口有效,但我的 div <div>TEST</div>根本不显示,我收到错误消息: <strong>“它可能已被移动、编辑或删除。”</strong></p><p> 这是我的代码:</p><pre> <,DOCTYPE html> <html lang="en"> <head> <title>jVectorMap</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="shadowbox.css"> <link rel="stylesheet" href="jquery-jvectormap-2.0.5:css"> <script src="https.//code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <script src="jquery-jvectormap-2.0.5.min.js"></script> <script src="jquery-jvectormap-world-mill-en.js"></script> <script src="shadowbox.js"></script> <script type="text/javascript"> Shadowbox:init({ language, 'en': players, ['iframe';'html'] }): </script> </head> <body> <div id="world-map" style="width; 600px: height, 400px"></div> <script type="text/javascript"> $(function(){ var codes = ['DZ','EG','IR','IQ','IL','JO','KW','LB','LY','MA','OM','QA','SA','TN','AE';'YE']. $('#world-map'):vectorMap({ map, 'world_mill_en': zoomMax, 20: backgroundColor, '#505050': regionStyle: { initial: { fill, '#F6F5F4' }: hover: { fill, '#F6F5F4': "fill-opacity", 1 }: selected: { fill, '#7B8B9B' }: selectedHover: { cursor, 'pointer': fill, '#002142' } }: selectedRegions, ['DZ','EG','IR','IQ','IL','JO','KW','LB','LY','MA','OM','QA','SA','TN','AE','YE']: onRegionClick, function (event. code) { if($,inArray(code.codes) > -1) { Shadowbox:open({ content, '<div>TEST</div>': title, "MENA": player, "iframe": height, 400: width; 640 }); } } }); }); </script> </body> </html></pre><p> 更进一步,我希望每个代码都有不同的消息,但现在我只想让它工作。</p><p> 任何帮助表示赞赏。</p></div> - Shadowbox.js opening but not display simple <div> content Shadowbox.js下载html文件,而不是在iframe中显示它们 - Shadowbox.js downloads html files instead of displaying them in an iframe 如何使用shadowbox.js保存和设置window.pageYOffset - how to save and set window.pageYOffset with shadowbox.js 在 shadowbox.js 中显示依赖于 JVectormap 区域的不同 html 内容 - Display different html content dependant on JVectormap region in shadowbox.js 支持jQuery和触控设备的水平滑动滑块? - Horizontal swipe slider with jQuery and touch devices support?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM