ReCaptcha API v2 Styling

前端 未结 18 1808
孤城傲影
孤城傲影 2020-11-28 02:59

I have not had much success finding how to style Google\'s new recaptcha (v2). The eventual goal is to make it responsive, but I am having difficulty applying styling for ev

18条回答
  •  自闭症患者
    2020-11-28 03:21

    Late to the party, but maybe my solution will help somebody.

    I haven't found any solution that works on a responsive website when the viewport changes or the layout is fluid.

    So I've created a jQuery script for django-cms that is dynamically adapting to a changing viewport. I'm going to update this response as soon as I have the need for a modern variant of it that is more modular and has no jQuery dependency.


    html


    css

    .g-recaptcha { display: none; }
    
    .g-recaptcha.g-recaptcha-initted { 
        display: block; 
        overflow: hidden; 
    }
    
    .g-recaptcha.g-recaptcha-initted > * {
        transform-origin: top left;
    }
    


    js

    window.djangoReCaptcha = {
        list: [],
        setup: function() {
            $('.g-recaptcha').each(function() {
                var $container = $(this);
                var config = $container.data();
    
                djangoReCaptcha.init($container, config);
            });
    
            $(window).on('resize orientationchange', function() {
                $(djangoReCaptcha.list).each(function(idx, el) {
                    djangoReCaptcha.resize.apply(null, el);
                });
            });
        },
        resize: function($container, captchaSize) {
            scaleFactor = ($container.width() / captchaSize.w);
            $container.find('> *').css({
                transform: 'scale(' + scaleFactor + ')',
                height: (captchaSize.h * scaleFactor) + 'px'
            });
        },
        init: function($container, config) {
            grecaptcha.render($container.get(0), config);
    
            var captchaSize, scaleFactor;
            var $iframe = $container.find('iframe').eq(0);
    
            $iframe.on('load', function() {
                $container.addClass('g-recaptcha-initted');
                captchaSize = captchaSize || { w: $iframe.width() - 2, h: $iframe.height() };
                djangoReCaptcha.resize($container, captchaSize);
                djangoReCaptcha.list.push([$container, captchaSize]);
            });
        },
        lateInit: function(config) {
            var $container = $('.g-recaptcha.g-recaptcha-late').eq(0).removeClass('.g-recaptcha-late');
            djangoReCaptcha.init($container, config);
        }
    };
    
    window.djangoReCaptchaSetup = window.djangoReCaptcha.setup;
    

提交回复
热议问题