How to stop focus() in custom js from scrolling to footer?

In jQuery focus by default scroll the page to the element. If we use focus({preventScroll: true}) our copy function will not work as you mension.
So I have use some css hack. See the following jQuery code.

function copyToClipboard(selector, event){
    event.preventDefault();
    var $temp = jQuery("<div>");
    jQuery("body").append($temp);
    $temp.attr("contenteditable", true)
      .html(jQuery(selector).html()).css('position', 'fixed').select()
      .on("focus", function() { document.execCommand('selectAll',false,null); })
      .focus().css('position', 'static');
    document.execCommand("copy");
    $temp.remove();
}

This is working fine for me.

If you don’t want to use this hack you can also use some pure javascript code. For example this