javascript - 在引导模式弹出窗口中旋转文本

标签 javascript jquery html css asp.net-mvc

我正在使用此代码在模型中旋转文本,但此代码在模型弹出窗口中不起作用?

  var rotation = 0;

        jQuery.fn.rotate = function (degrees) {
            $(this).css({
                '-webkit-transform': 'rotate(' + degrees + 'deg)',
                '-moz-transform': 'rotate(' + degrees + 'deg)',
                '-ms-transform': 'rotate(' + degrees + 'deg)',
                'transform': 'rotate(' + degrees + 'deg)'
            });
        };

        $('#btnRotateRight').click(function () {
            rotation += 5;
            $('.rotate').rotate(rotation);
        });

        $('#btnRotateLeft').click(function () {
            rotation -= 5;
            $('.rotate').rotate(rotation);
        });

然后下面的代码是模型弹出。

 $("#ViewDetail").append('<div class="modal-content"><div class="modal-header"><button id="btnRotateLeft" type="button">Rotate Left</button> <button id="btnRotateRight" type="button">Rotate Right</button><button type="button" class="close" data-dismiss="modal">&times;</button><h4 class="modal-title">Preview Cake</h4> </div> <div class="modal-body" style="text-align:center;"><div style="background-image:url(' + data.PrevRoundImg + ');max-width:320px;height:320px;background-repeat: no-repeat;padding-top:40%;" ><div class="rotate"><b id="msgtext" style="font-family:cursive;color:black; text-shadow: 0 0 3px White;font-size:20px;" >' + ckemsg + '</b></div></div></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div>');

最佳答案

您的脚本无法正常工作,因为您正在注册事件 $('#btnRotateRight').click($('#btnRotateLeft').click( on dynamic内容,因此对于这种情况,您需要在页面中呈现元素后注册事件或使用 Jquery on 事件,如下所示:

    $(document).on("click", "#btnRotateRight", function () {
        rotation += 5;
        $('.rotate').rotate(rotation);
    });

    $(document).on("click", "#btnRotateLeft", function () {
        rotation -= 5;
        $('.rotate').rotate(rotation);
    });

或者在渲染页面中的元素后注册事件,如下所示:

    $("#btnCheck").click(function () {
        $("#ViewDetail").append('<div class="modal-content"><div class="modal-header"><button id="btnRotateLeft" type="button">Rotate Left</button> <button id="btnRotateRight" type="button">Rotate Right</button><button type="button" class="close" data-dismiss="modal">&times;</button><h4 class="modal-title">Preview Cake</h4> </div> <div class="modal-body" style="text-align:center;"><div style="background-image:url(' + data.PrevRoundImg + ');max-width:320px;height:320px;background-repeat: no-repeat;padding-top:40%;" ><div class="rotate"><b id="msgtext" style="font-family:cursive;color:black; text-shadow: 0 0 3px White;font-size:20px;" >' + ckemsg + '</b></div></div></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div>');
        RegisterEvents();
    });
    var rotation = 0;
    function RegisterEvents() {
        jQuery.fn.rotate = function (degrees) {
            $(this).css({
                '-webkit-transform': 'rotate(' + degrees + 'deg)',
                '-moz-transform': 'rotate(' + degrees + 'deg)',
                '-ms-transform': 'rotate(' + degrees + 'deg)',
                'transform': 'rotate(' + degrees + 'deg)'
            });
        };
        $('#btnRotateRight').click(function () {
            rotation += 5;
            $('.rotate').rotate(rotation);
        });
        $('#btnRotateLeft').click(function () {
            rotation -= 5;
            $('.rotate').rotate(rotation);
        });
    }

关于javascript - 在引导模式弹出窗口中旋转文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37521327/

相关文章:

javascript - 客户端的 jQuery 文件扩展名验证

javascript - 向上滚动时获取元素的滚动位置

javascript - 计算绝对定位嵌套子项的高度

javascript - 无法实现无限滚动

javascript - Stripe支付网关在rails上发布ruby(TypeError : window.多媒体(...)为空)

javascript - 加载前检查图片是否存在

javascript - 使用 jQuery 根据 AJAX 结果修改 DOM

javascript - jquery- 在 div 的顶部保留图像

html - 对齐页脚的方法?

javascript - 使用 jquery 覆盖 anchor 标记的默认行为