Fancybox中的图像旋转

4
我正在创建一个界面,允许用户将图像逆时针旋转90度。我使用jquery和-webkit-transform在页面上旋转图像,但我也希望更新Fancybox幻灯片中图像的预览。
我尝试通过以下方式旋转图像:
 $(".fancybox").fancybox({           
      afterShow: function(){
        fancyboxRotation();
      }
    });

function fancyboxRotation(){
    $('.fancybox-wrap').css('webkitTransform', rotate(-90deg));
    $('.fancybox-wrap').css('mozTransform', rotate(-90deg));
}

但是这样做会导致控件也旋转了(并且将关闭按钮放在了左上角而不是右上角):

enter image description here

如果我只是将旋转应用于图像,则其周围的白色边框方向错误:

enter image description here

有没有经验将变换应用于fancybox图像的人?

8个回答

3
对于fancybox 3,以下是我想到的方案。它使用font awesome图标,您可以替换为glyphicons或其他您选择的任何内容。
//adding custom item to fancybox menu to rotate image
    $(document).on('onInit.fb', function (e, instance) {
        if ($('.fancybox-toolbar').find('#rotate_button').length === 0) {
            $('.fancybox-toolbar').prepend('<button id="rotate_button" class="fancybox-button" title="Rotate Image"><i class="fa fa-repeat"></i></button>');
        }
        var click = 1;
        $('.fancybox-toolbar').on('click', '#rotate_button', function () {
                var n = 90 * ++click;
                $('.fancybox-image-wrap img').css('webkitTransform', 'rotate(-' + n + 'deg)');
                $('.fancybox-image-wrap img').css('mozTransform', 'rotate(-' + n + 'deg)');
            });
    });

2
你可以旋转fancy box内容中最外层的div,在我的情况下,它是fancybox-skin(fancybox v2)。
afterShow: function(){
    var click = 1;
    $('.fancybox-wrap').append('<div id="rotate_button"></div>')
        .on('click', '#rotate_button', function(){
            var n = 90 * ++click;
            $('.fancybox-skin').css('webkitTransform', 'rotate(-' + n + 'deg)');
            $('.fancybox-skin').css('mozTransform', 'rotate(-' + n + 'deg)');
        });
};

这并没有正确地调整图像的大小,也没有将按钮放置在预期位置。 - Joost

1
通过@Ashik的帮助,我终于把这个东西搞定了,也不必放弃显示标题,因为我旋转了.fancybox-inner并覆盖了一些CSS,这样就可以保留白色边框。我还从$(document).ready()函数中初始化了fancybox,所以我必须有点不同地绑定按钮。
最后,这是一个比较长的答案,如果我漏掉了什么,请告诉我,因为完全有可能!此外,我们不需要支持IE(感谢上帝),所以它可能或可能不会在那里正常工作。
我去掉了普通的箭头和关闭按钮,这需要您添加fancy box按钮助手CSS和JS文件:
<link href="/Content/css/jquery.fancybox.css" rel="stylesheet"/>
<link href="/Content/css/jquery.fancybox-buttons.css" rel="stylesheet"/>

<script src="/Content/Scripts/fancybox/jquery.fancybox.js"></script>
<script src="/Content/Scripts/fancybox/jquery.fancybox.pack.js"></script>
<script src="/Content/Scripts/fancybox/jquery.fancybox-buttons.js"></script>

然后,初始化fancy box是在$(document).ready()中完成的,就像我说的那样,如下所示(请注意,我删除了箭头和关闭按钮,并使用按钮助手的tpl属性添加它们回来)。在tpl属性中,我还创建了一个自定义旋转按钮,其中包含一个onclick和一个自定义的data-rotation属性,用于保存当前旋转状态:
$(document).ready(function() {

    $(".fancybox").fancybox({
        loop   : true,
        helpers: {
            buttons: {
                position: 'top',
                tpl     : '<div id="fancybox-buttons"><ul><li><a class="btnPrev" title="Previous" href="javascript:;"></a></li><li><a id="fancybox-rotate-button" title="Rotate" data-rotation="0" onclick="FancyBoxRotateButton()"></a></li><li><a class="btnNext" title="Next" href="javascript:;"></a></li><li><a class="btnClose" title="Close" href="javascript:jQuery.fancybox.close();"></a></li></ul></div>'
            }
        },
        closeBtn: false, // you will use the tpl buttons now
        arrows  : false  // you will use the tpl buttons now
    });

这是自定义旋转按钮的onclick函数:

window.FancyBoxRotateButton = function() {
    var fancyboxInner        = $('.fancybox-inner');
    var fancyBoxRotateButton = $('#fancybox-rotate-button');
    var currentRotation      = parseInt(fancyBoxRotateButton.data("rotation"));
    var rotation             = 'rotate(-' + (90 * ++currentRotation) + 'deg)';

    fancyboxInner.css({
        '-moz-transform'   : rotation,
        '-webkit-transform': rotation,
        '-o-transform'     : rotation,
        'transform'        : rotation
    });

    fancyBoxRotateButton.data("rotation", currentRotation.toString());
}

最后,我们需要修复白色边框,然后我还要更改自定义按钮
    的大小,并设置自定义旋转按钮的图片。可能有更好的方法来做到这一点(如果您知道其中之一,请告诉我!),但我只是删除了.fancybox-skin的背景和盒子阴影,并将其添加到.fancybox-inner中:
    #fancybox-buttons ul{
        width: 130px;
    }
    
    #fancybox-buttons #fancybox-rotate-button {
        background-image: url('/Content/images/fancybox_rotate.png')
    }
    
    .fancybox-skin {
        background: none !important;
    }
    
    .fancybox-opened .fancybox-skin {
        -webkit-box-shadow: none !important;
        -moz-box-shadow   : none !important;
        box-shadow        : none !important;
    }
    
    
    .fancybox-inner {
        border-radius     : 4px;
        border            : 2px solid white;
        padding           : 10px;
        background        : white none repeat scroll 0 0;
        -webkit-box-shadow: 0 10px 25px #000000;
        -webkit-box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
        -moz-box-shadow   : 0 10px 25px #000000;
        -moz-box-shadow   : 0 10px 25px rgba(0, 0, 0, 0.5);
        box-shadow        : 0 10px 25px #000000;
        box-shadow        : 0 10px 25px rgba(0, 0, 0, 0.5);
    }
    

    希望能对某人有所帮助!

0
我的解决方案是从fancybox中删除箭头和关闭按钮,并在整个fancybox-wrap应用旋转后淡入fancybox幻灯片。为了做到这一点,我在“beforeShow”中将fancybox-wrap的显示设置为none,然后在“AfterShow”中淡入图像。以下是我的代码:
    $(".fancybox").fancybox({
      helpers: {
        overlay: {
          locked: false
        }
      },
      arrows: false,
      closeBtn: false,
      beforeShow: function(){
        if($('.fancybox-image').length>0){
           $('.fancybox-wrap').css('display', 'none');
            var imageID = getFancyboxImageID();
            var rotation = getFancyboxRotation(imageID);
            if(rotation!=0){
              fancyboxRotate(rotation);  
            }
        }
      },
      afterShow: function(){
        if($('.fancybox-image').length>0){
          $('.fancybox-wrap').fadeIn();
        }
      }
    });

0
你可以通过将CSS样式-webkit-transform: rotate(-90deg);应用于'.fancybox-inner'类元素而不是'.fancybox-wrap'来旋转图像,这样它只会旋转图像而不是包含控件和描述的整个容器。

感谢您的回复。当我仅对图像应用变换时,出现了上面第二张图片中显示的问题,即其周围的白色边框大小不正确。 - scientiffic

0

在Github上查看此问题:https://github.com/fancyapps/fancybox/issues/1100

用户seltix5的代码在fancybox v3中有效。我亲自测试过。

您只需要将他/她的代码附加到您的fancybox.js文件中即可。它会在“onInit.fb”事件后简单地扩展fancybox对象以实现旋转功能。它还会在顶部菜单中添加旋转按钮和平滑旋转动画。


0

我想保留关闭按钮和标题,所以我用 CSS3 变换做了一个小魔术:

afterShow: function() {
    var click = 0, deg;
    $('.fancybox-inner')
        .append('<img id="rotate_button" src="https://cdn0.iconfinder.com/data/icons/super-mono-sticker/icons/button-rotate-cw_sticker.png" title="Rotate 90° CW">')
        .on('click', '#rotate_button', function() {
            click = (++click % 4 === 0) ? 0 : click;
            deg = 90 * click;
            $('.fancybox-wrap').css('transform', 'rotate(' + deg + 'deg)');
            $('#rotate_button').css('transform', 'rotate(-' + deg + 'deg)');
            sessionStorage.setItem('prev_rotated_image', $('.fancybox-image').prop('src'));
            sessionStorage.setItem($('.fancybox-image').prop('src'), deg);

            // move the close button and rotate the label
            switch (deg) {
                case 90:
                    $('.fancybox-close').css('transform', 'translate(-' + $('.fancybox-wrap').width() + 'px, 0px)');
                    $('.fancybox-title').find('span.child').css('transform', 'translate(' + ($('.fancybox-wrap').width() / 2 + $('.fancybox-title').height() / 2 + 8) + 'px, -' + ($('.fancybox-wrap').height() / 2) + 'px) rotate(-' + deg + 'deg)');
                    break;
                case 180:
                    $('.fancybox-close').css('transform', 'translate(-' + $('.fancybox-wrap').width() + 'px, ' + $('.fancybox-wrap').height() + 'px)');
                    $('.fancybox-title').find('span.child').css('transform', 'translate(0px, -'+ ($('.fancybox-wrap').height() + $('.fancybox-title').height() + 16) +'px) rotate(-' + deg + 'deg)');
                    break;
                case 270:
                    $('.fancybox-close').css('transform', 'translate(0px, ' + $('.fancybox-wrap').height() + 'px)');
                    $('.fancybox-title').find('span.child').css('transform', 'translate(-' + ($('.fancybox-wrap').width() / 2 + $('.fancybox-title').height() / 2 + 8) + 'px, -' + ($('.fancybox-wrap').height() / 2) + 'px) rotate(-' + deg + 'deg)');
                    break;
                case 0:
                case 360:
                default:
                    $('.fancybox-close').css('transform', 'translate(0px, 0px)');
                    $('.fancybox-title').find('span.child').css('transform', 'translate(0px, 0px) rotate(0deg)');
            }
        });
}

0

感谢@Paul提供的精彩代码片段,我已经添加了一些类别更改(当前幻灯片)和CSS属性,这对我的fancybox3版本有用。可能会帮助到其他人。

注意:您可以使用图标替换“旋转”文本。

//adding custom item to fancybox menu to rotate image
$(document).on('onInit.fb', function (e, instance) {
    if ($('.fancybox-toolbar').find('#rotate_button').length === 0) {
        $('.fancybox-toolbar').prepend('<button id="rotate_button" class="fancybox-button" title="Rotate Image">Rotate</button>');
    }
    var click = 1;
    $('.fancybox-toolbar').on('click', '#rotate_button', function () {
            var n = 90 * ++click;
            $('.fancybox-slide--current img').css('webkitTransform', 'rotate(-' + n + 'deg)');
            $('.fancybox-slide--current img').css('mozTransform', 'rotate(-' + n + 'deg)');
            $('.fancybox-slide--current img').css('transform', 'rotate(-' + n + 'deg)');
        });
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接